Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <cctype>
- #include <cstdio>
- using namespace std;
- struct Inventory {
- string description;
- int quantity;
- double wholesaleCost;
- double retailCost;
- string dateAdded;
- };
- void prompt();
- void addItem(fstream &);
- void viewItem(fstream &);
- void modifyItem(fstream &);
- void validate(int *);
- void validate(double *);
- string getDate();
- string validateDate(string);
- int main() {
- char userChoice;
- fstream invFile("inventory.txt", ios::in | ios::out | ios::app);
- if (invFile.fail()) // creates file if a file does not already exist
- {
- invFile.open("inventory.txt");
- invFile.close();
- }
- do {
- prompt();
- cin >> userChoice;
- while(!isdigit(userChoice)) {
- cout << "Enter a digit only.\nEnter choice: ";
- cin >> userChoice;
- }
- switch(userChoice) {
- case '1':
- addItem(invFile);
- break;
- case '2':
- viewItem(invFile);
- break;
- case '3':
- modifyItem(invFile);
- break;
- }
- } while(userChoice != '4');
- invFile.close();
- return 0;
- }
- void prompt() {
- cout << "Choose from the following.\n";
- cout << "1. Add new record(s) to the file.\n";
- cout << "2. Display any record(s) in the file.\n";
- cout << "3. Change any record(s) in the file.\n";
- cout << "4. Exit\n";
- cout << "Enter choice: ";
- }
- void addItem(fstream& file) {
- Inventory item;
- string date;
- cout << "Enter the description of the item: ";
- cin >> item.description;
- file << "Description: " << item.description << endl;
- string itemDesc = item.description;
- cout << "Enter the quantity of " << itemDesc << ": ";
- cin >> item.quantity;
- validate(&item.quantity);
- file << "Quantity: " << item.quantity << endl;
- cout << "Enter the Wholesale Cost of " << itemDesc << ": $";
- cin >> item.wholesaleCost;
- validate(&item.wholesaleCost);
- file << "Wholesale Cost $" << item.wholesaleCost << endl;
- cout << "Enter the Retail Cost of " << itemDesc << ": $";
- cin >> item.retailCost;
- validate(&item.retailCost);
- file << "Retail Cost $" << item.retailCost << endl;
- item.dateAdded = getDate();
- file << "Date added: " << item.dateAdded << "\n\n";
- }
- void viewItem(fstream& file) {
- file.close();
- file.open("inventory.txt");
- // closing and re-opening the file helps ensure that the file is being read from the beginning
- string desc, line;
- cout << "Enter the name of the item you want to display from the record(s): ";
- cin >> desc,
- desc = "Description: " + desc;
- getline(file, line); // gets the first line of the file
- while(true) {
- if(line == desc) { // if the matching item is found, it will display the 5 lines of corresponding info on that item
- for(int i = 0; i < 6; i++) {
- cout << line << endl;
- getline(file, line);
- }
- }
- getline(file, line); // gets the next line of the file
- if(file.eof()) { // conditional to break out of the loop once the end of the file is reached
- break;
- }
- }
- }
- void modifyItem(fstream& file) {
- fstream temp("temp.txt", ios::out);
- string desc, line;
- cout << "Enter the name of the item you want to modify from the record(s): ";
- cin >> desc,
- desc = "Description: " + desc;
- getline(file, line);
- while(line != desc) {
- temp << line << endl;
- getline(file, line);
- }
- if(line == desc) {
- addItem(temp);
- }
- while(true) {
- if(line == desc) {
- for(int i = 0; i < 6; i++) {
- getline(file, line);
- }
- }
- temp << line << endl;
- getline(file, line);
- if(file.eof()) {
- break;
- }
- }
- file.close();
- temp.close();
- remove("inventory.txt");
- rename("temp.txt", "inventory.txt");
- if( remove( "temp.txt" ) != 0 )
- perror( "Error deleting file" );
- else
- puts( "File successfully deleted" );
- }
- string getDate() {
- string date;
- cout << "Enter the date that the item was added to the inventory (mm/dd/yyyy): ";
- cin >> date;
- date = validateDate(date);
- return date;
- }
- string validateDate(string date) {
- for(int i = 0; i < 11; i++) {
- if(i == 0 || i == 1 || i == 3 || i == 4 || i == 6 || i == 7 || i == 8 || i == 9) {
- if(!(isdigit(date[i]))) {
- cout << "Invalid date format!\n";
- getDate();
- }
- }
- else if(i == 2 || i == 5) {
- if(date[i] != '/') {
- cout << "Invalid date format!\n";
- getDate();
- }
- }
- }
- return date;
- }
- void validate(int *num) {
- while(*num < 0) {
- cout << "No negative numbers allowed.\nPlease enter a non-negative value: ";
- cin >> *num;
- }
- }
- // validate function that takes a double as a parameter
- void validate(double *num) {
- while(*num < 0) {
- cout << "No negative numbers allowed.\nPlease enter a non-negative value: ";
- cin >> *num;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement