Question: validate the records car Id must be 9 characters long with the first two numeric only (0-9), the next four alpha characters, and the other

validate the records car Id must be 9 characters long with the first two numeric only (0-9), the next four alpha characters, and the other 3 characters may be alpha-numeric ( for example, 12ABMP34Z- valid, 35KMOP324 - invalid); the only alpha characters allowed are A-S and letter O is not allowed

add a function(s) to validate a record (isValidRec()), modify getData() so it calls isValidRec() and stores good records in the array of records and stores bad records in the error file, test again

code I currently have:

#include #include #include #include using namespace std;

const int MAX_ID_LENGTH = 9 ; // structure containing carID, model, quantity and price typedef struct { string carID; string model; string quantity; string price; } CarRecord;

// Function to check if a car record is valid bool isValidCarRecord(CarRecord car) {

}

// swap two structures, this helps in bubble sort function void swap(CarRecord *xp, CarRecord *yp) { CarRecord temp = *xp; *xp = *yp; *yp = temp; } // Searching By carID and if found print details else print not found void searchBycarID(CarRecord inventory[100], int size) { string carID; cout << "Please give carID :"; // taking carID from user cin >> carID; for (int i = 0; i < size; i++) { // looping through whole inventory if (inventory[i].carID == carID) // checking if carID matches, if yes print results and return cout << inventory[i].carID << "\t" << inventory[i].model << "\t" << inventory[i].quantity << "\t" << inventory[i].price << endl << endl; return; } cout << "carID not found. "; } // sorting using bubble sort. void sort(CarRecord inventory[100], int size, int sortBy) { int i, j; bool condition; for (i = 0; i < size - 1; i++) { for (j = 0; j < size - i - 1; j++) { // checking condition based in user selection if (sortBy == 0) { condition = inventory[j].carID > inventory[j + 1].carID; } else if (sortBy == 1) { condition = inventory[j].model > inventory[j + 1].model; } else if (sortBy == 2) { condition = inventory[j].quantity > inventory[j + 1].quantity; } else if (sortBy == 3) { condition = inventory[j].price > inventory[j + 1].price; } if (condition) { // if condition is true swap swap(&inventory[j], &inventory[j + 1]); } } } } // reading file void getData(CarRecord inventory[100], int *size) { ifstream file("carInventory.txt"); if (file.is_open()) { string line; // reading file line by line while (getline(file, line)) { size_t pos = 0; string token; CarRecord temp; pos = line.find(" "); // finding position of space token = line.substr(0, pos); // getting substring from start to space location temp.carID = token; // first is carID line.erase(0, pos + 1); // removing carID part from line // repeating process for model,price and quantity pos = line.find(" "); token = line.substr(0, pos); temp.model = token; line.erase(0, pos + 1); pos = line.find(" "); token = line.substr(0, pos); temp.quantity = token; line.erase(0, pos + 1); temp.price = line; inventory[*size] = temp; *size = *size + 1; } file.close(); } else { cout << "Cannot open file. "; EXIT_SUCCESS; } } // printing inventory void printCarInventory(CarRecord inventory[100], int size, bool valid) { if(valid == false){ cout << left << setw(10) << "Car ID" << setw(20) << "Model" << right << setw(10) << "Quantity" << setw(10) << "Price" << endl; cout << left << setw(9) << setfill('-') << "-" << setw(20) << setfill('-') << "-" << right << setw(10) << setfill('-') << "-" << setw(10) << setfill('-') << "-" << endl; cout << setfill(' '); for (int i = 0; i < size; i++) { cout << left << setw(9) << inventory[i].carID << "\t" <> choice; // take choice again for invalid input while (choice < 1 || choice > 8) { cout << "Please choose valid input: "; cin >> choice; } // clearing cin cin.clear(); cin.ignore(); return choice; }

void printInvalidCarRecords(string fileName, CarRecord inventory[100], int size){ ofstream fout; fout.open("error.txt", ios::app); for (int i = 0; i < size; i++) { fout << inventory[i].carID << "\t" << inventory[i].model << "\t" << inventory[i].quantity << "\t" << inventory[i].price<< endl; } fout.close(); }

int main() { bool valid; // array of structure CarRecord CarRecord inventory[100]; int size = 0; int choice = 1; // reading file and storing in array getData(inventory, &size); // continue untill user chooses to exit while (choice != 8) { choice = printMenu(); switch (choice) { case 1: // for case 1 print inventory printCarInventory(inventory, size, valid); break; case 2: // for case 2 print invalid inventory printInvalidCarRecords("carInventory.txt", inventory, size ); break; case 3: // for input 2 search inventory by carID searchBycarID(inventory, size); break; case 4: // sort inventory by carID sort(inventory, size, 0); break; case 5: // sort inventory by model sort(inventory, size, 1); break; case 6: // sort inventory by quantity sort(inventory, size, 2); break; case 7: // sort inventory by price sort(inventory, size, 3); break; case 8: cout<< "Goodbye"; break; default: // printing message for invalid input cout << "Invalid Input. Please try again. "; break; } } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!