Question
Can do a flowchart for this C++ code? #include #include #include #include #include //declare a string stream ( a sort of string that can write
Can do a flowchart for this C++ code?
#include
//global variable int quantity1=20, quantity2=30, quantity3=25, quantity4=55 , password, account; char name[100]; ifstream in; string line; ofstream acc; //this is temporary file where the record will be stored temporarily
struct vending { char name[20]; float price; unsigned quantity; };
// function prototype void vendor(); void vendorAccount(); void createVendorAccount(); void loginVendorAcc(); void updateItems(); void readVendorFile(); void customer(); void customerAccount(); void createCustomerAccount(); void loginCustomerAcc(); void readCustomerFile(); void find(); void input();
int main() //main body { int choice = 1; system("cls"); cout << " 1) Vendor Account"; cout << " 2) Customer Account"; cout << " Choose account type:"; cin >> choice;
if (choice == 1) vendor(); else if (choice == 2) customer(); }
void vendor() { system("cls"); int choice = 1; cout << " 1) Create vendor account"; cout << " 2) Login vendor account"; cout << " 3) Back to main menu"; cout << " Choose action:"; cin >> choice;
if (choice == 1) createVendorAccount(); else if (choice == 2) loginVendorAcc(); else if (choice == 3) main(); }
void createVendorAccount(){ system("cls"); cout << endl; cout << "Creat Vendor Account "; cout << "Please enter your, Account Number : "; cin >> account; cout << endl; cout << " Password (Numbers Only) : "; cin >> password; cout << endl; ofstream VendorFile("Vendor.txt"); if(VendorFile.is_open()) { VendorFile << account << " " << password<< endl; //write the info into the text file VendorFile.close(); } else cout << "File could not be opened"; //shown if the file cannot be opened vendor(); }
void readVendorFile() // function to open and read from the vendor text file { string line; ifstream VendorFile("Vendor.txt"); if(VendorFile.is_open()) { while (getline(VendorFile, line)) // search for the line that you want to update { istringstream ss(line); //create a string line called ss consisting of the string value ss >> account >> password; // extract/read the string from the text file } VendorFile.close(); } else cout << "File could not be opened"; }
void loginVendorAcc() { int vaccNo, vlgpassword; system("cls"); readVendorFile(); // open and read the vendor text file cout << endl; cout << "VENDOR LOGIN "; cout << "Please enter your, account number: "; cin >> vaccNo; if (vaccNo!=account) // compare the vendor acc { do //progrom continue looping unless correct acc is entered { cout << "Invalid account. Please try again."<< endl; cout << "Please enter your, account number: "; cin >> vaccNo; } while (vaccNo!=account); } cout << "Please enter your password :" ; cin >> vlgpassword; if (vlgpassword!=password ) //return to initial part of vendor login as the password entered is wrong { cout << "Invalid password. "<< endl; loginVendorAcc(); }
vendorAccount(); //if both acc and password is correct, proceed to update stock }
void vendorAccount() { system("cls"); int choice = 1; cout << " 1) Update account information"; cout << " 2) Update item quantity"; cout << " Choose action:"; cin >> choice;
if (choice == 1) input(); else if (choice == 2) updateItems(); }
void updateItems() { system("cls"); int choice = 1; int quantity = 0; while(choice!=5){ system("cls"); cout << " 1) Snack bar"; cout << " 2) Chips"; cout << " 3) Water"; cout << " 4) Soft drink"; cout << " 5) Quit"; cout << " Choose item to update:"; cin >> choice; if (choice == 5) main(); if (choice >= 1 && choice <= 4) { cout << " New Quantity:"; cin >> quantity; if (choice == 1) quantity1 = quantity; else if (choice == 2) quantity2 = quantity; else if (choice == 3) quantity3 = quantity; else if (choice == 4) quantity4 = quantity; } } }
void customer() { system("cls"); int choice = 1; cout << " 1) Create customer account"; cout << " 2) Login customer account"; cout << " 3) Back to main menu"; cout << " Choose action:"; cin >> choice;
if (choice == 1) createCustomerAccount(); else if (choice == 2) loginCustomerAcc(); else if (choice == 3) main(); }
void createCustomerAccount(){ system("cls"); cout << endl; cout << "Creat Customer Account "; cout << "Please enter your, Account Number : "; cin >> account; cout << endl; cout << " Password (Numbers Only) : "; cin >> password; cout << endl; ofstream CustomerFile("Customer.txt"); if(CustomerFile.is_open()) { CustomerFile << account << " " << password << endl; // write the info into the text file
CustomerFile.close(); } else cout << "File could not be opened"; //shown if the file cannot be opened customer(); }
void readCustomerFile() // function to open and read from the vendor text file { string line; ifstream CustomerFile("Customer.txt"); if(CustomerFile.is_open()) { while (getline(CustomerFile, line)) // search for the line that you want to update { istringstream ss(line); //create a string line called ss consisting of the string value ss >> account >> password; // extract/read the string from the text file } CustomerFile.close(); } else cout << "File could not be opened"; }
void loginCustomerAcc() { int accNo, lgpassword; system("cls"); readCustomerFile(); // open and read the vendor text file cout << endl; cout << "Customer LOGIN "; cout << "Please enter your, account number: "; cin >> accNo; if (accNo!=account) // compare the vendor acc { do //progrom continue looping unless correct acc is entered { cout << "Invalid account. Please try again."<< endl; cout << "Please enter your, account number: "; cin >> accNo; } while (accNo!=account); } cout << "Please enter your password :" ; cin >> lgpassword; if (lgpassword!=password ) //return to initial part of vendor login as the password entered is wrong { cout << "Invalid password. "<< endl; loginCustomerAcc(); }
customerAccount(); //if both acc and password is correct, proceed to update stock }
void customerAccount() { system("cls"); vending item[5];
strcpy(item[0].name, "Snack bar");item[0].price = 1.50;item[0].quantity = quantity1; strcpy(item[1].name, "Chips");item[1].price = 2.00;item[1].quantity = quantity2; strcpy(item[2].name, "Water");item[2].price = 1.00;item[2].quantity = quantity3; strcpy(item[3].name, "Soft drink");item[3].price = 2.50;item[3].quantity = quantity4;
cout << fixed; cout << setprecision(2);
float money = 1.00; float totalPrice = 0;
while (fmod(money, 5.00) != 0) { cout << " Deposits money (credit), only RM5.00 and RM10.00 are acceptable: "; cin >> money; }
int choice = 1;
do { system("cls"); cout << " 1) " << item[0].name << "\t\t RM " << item[0].price << "\t(" << item[0].quantity << ") remaining"; cout << " 2) " << item[1].name << "\t\t RM " << item[1].price << "\t(" << item[1].quantity << ") remaining"; cout << " 3) " << item[2].name << "\t\t RM " << item[2].price << "\t(" << item[2].quantity << ") remaining"; cout << " 4) " << item[3].name << "\t\t RM " << item[3].price << "\t(" << item[3].quantity << ") remaining"; cout << " 5) Return to main menu "; cout << " Choose one:"; cin >> choice;
if (choice >= 1 && choice <= 4) { if (item[choice - 1].quantity == 0) { cout << " No more " << item[choice - 1].name << " Available .."; getchar(); getchar(); continue; } }
if (choice == 5) cout << " Thank you for using it !!"; else if (choice <= 4) {
if (choice >= 1 && choice <= 4) { totalPrice += item[choice - 1].price; // tp = tp + price if ((money < totalPrice)) { cout << " Insufficient credit. You need at least RM" << totalPrice - money << " to continue purchasing"; totalPrice -= item[choice - 1].price; getchar(); getchar(); continue; } }
item[choice - 1].quantity = item[choice - 1].quantity - 1; cout << " There are " << item[choice - 1].quantity << " items of that type left";
getchar(); getchar();
} else { cout << " Warning : Invalid Choice "; } } while (choice != 5); cout << " Enjoy your beverage "; cout << " Change calculated : RM " << money - totalPrice; cout << " Your change, RM " << money - totalPrice << " just droped into the Change Dispenser. "; getchar(); getchar(); main(); }
void input() { ofstream out; acc.open("temp.txt",ios::app); in.open("Vendor.txt");
if(in.is_open()) { while(getline(in,line)) { acc< acc.close(); in.close(); } find(); } void find() { system("cls"); in.open("Vendor.txt"); acc.open("temp.txt"); while(in>>account>>password) { if(account==account) { cout<<"Account Number: " << account << "\t" << "Password: " < cout<<"Enter the new password"< remove("Vendor.txt"); rename("temp.txt","Vendor.txt"); } } vendor(); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started