1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
#include <fstream>
#include <iostream>
#include <iomanip>
#include <cctype>
struct carInfo {
std::string ID;
std::string Model;
int Qty {};
double Price {};
bool Valid {};
};
const size_t arraySize { 100 };
size_t getData(carInfo cars[arraySize]);
void welcome(const carInfo cars[], size_t norec);
int main() {
carInfo cars[arraySize] {};
const auto norec { getData(cars) };
welcome(cars, norec);
}
bool isValid(const std::string& testID, const std::string& testName, int testQty, double testPrice) {
size_t digit {};
//checks if ID is valid
for (size_t i {}; i < testID.length(); ++i) {
++digit;
if (digit <= 2 && std::isdigit(static_cast<unsigned char>(testID[i])) == false)
return false;
if (digit > 2 && digit <= 6 && (std::isalpha(static_cast<unsigned char>(testID[i])) == false || testID[i] == 'O'))
return false;
if (digit > 6 && digit <= 9 && testID[i] == 'O')
return false;
}
if (digit != 9)
return false;
//Checks if Name is Valid
digit = 0;
for (size_t i {}; i < testName.length(); ++i) {
++digit;
if (digit == 1 && !std::isupper(static_cast<unsigned char>(testName[i])))
return false;
if (std::isalpha(static_cast<unsigned char>(testName[i - 1])) && std::isdigit(static_cast<unsigned char>(testName[i])))
return false;
if (digit > 1 && (std::isupper(static_cast<unsigned char>(testName[i])) && testName[i - 1] != '_'))
return false;
}
//Checks if Quantity is Valid
if (testQty < 0)
return false;
//Checks if Price is Valid
if (testPrice <= 0)
return false;
return true;
}
size_t getData(carInfo cars[arraySize]) {
std::ifstream inputFile("Records.txt");
if (!inputFile) {
std::cout << "Input file not found. Quitting program.\n";
exit(EXIT_FAILURE);
}
std::ofstream errorFile("Errors.txt");
if (!errorFile) {
std::cout << "Error file not found. Quitting program.\n";
exit(EXIT_FAILURE);
}
size_t carNum {};
for (carInfo car; carNum < arraySize && (inputFile >> car.ID >> car.Model >> car.Qty >> car.Price); ++carNum) {
car.Valid = isValid(car.ID, car.Model, car.Qty, car.Price);
cars[carNum] = car;
if (!cars[carNum].Valid)
errorFile << car.ID << " " << car.Model << " " << car.Qty << " " << car.Price << '\n';
}
return carNum;
}
void invalidInput() {
std::cout << "Invalid input. Please try again.\n";
}
void printItems(const carInfo cars[], size_t nocars, bool valid) {
std::cout << std::setw(15) << "ID" << std::setw(15) << "Model" << std::setw(15) << "Qty"
<< std::setw(15) << "Price" << '\n';
for (size_t i {}; i < nocars; ++i)
if (valid == cars[i].Valid)
std::cout << std::setw(15) << cars[i].ID << std::setw(15) << cars[i].Model << std::setw(15) << cars[i].Qty
<< std::setw(15) << std::setprecision(2) << std::fixed << cars[i].Price << '\n';
}
void welcome(const carInfo cars[], size_t norec) {
int option {};
enum menuSelect {
valid = 1, invalid = 2, exit = 3
};
while (option != exit) {
std::cout << "\nHello there!\nHow may I help you?\n\n";
std::cout << "Press 1 to print valid items in Inventory\nPress 2 to print "
"invalid items in Inventory\nPress 3 to Exit\n\n";
std::cin >> option;
switch (option) {
case valid:
printItems(cars, norec, true);
break;
case invalid:
printItems(cars, norec, false);
break;
case exit:
break;
default:
invalidInput();
break;
}
}
std::cout << "Exiting now... Until next time!\n\n";
}
| |