#include #include #include using namespace std; int items {0}; double total {0.0}; void machine(double p); int main(){ struct snack { string name; double price; string id; }; const snack c[]{ {"Chips", 4.99, "A1"}, {"Candy bar", 1.99, "A2"}, {"Can of soda", 2.99, "A3"}, {"Bottle of water", 0.99, "A4"}, {"Candy", 2.99, "A5"} }; for(int i {0}; i < 5; i++){ cout << c[i].name << ", " << c[i].price << ", " << c[i].id <<"\n"; } string b {""}; while (b != "A0"){ cout << "Please input the id for each item you want. Press 'A0' to cancel.\n"; cin >> b; if (!cin){ cerr << "Invalid input. Please try again.\n"; continue; } else if (b == "A0"){ continue; } else if (b != "A1" && b != "A2" && b != "A3" && b != "A4" && b != "A5"){ cerr << "Invalid input. Please try again.\n"; continue; } if(b == "A1"){ machine(c[0].price); } else if(b == "A2"){ machine(c[1].price); } else if(b == "A3"){ machine(c[2].price); } else if(b == "A4"){ machine(c[3].price); } else{ machine(c[4].price); } cout << "Would you like anything else? (Y/N)\n"; string ans {""}; cin >> ans; while(ans != "N" && ans != "Y"){ cerr << "Invalid reply. Would you like another item? (Y/N)\n"; cin >> ans; } if(ans == "N"){ break; } else{ continue; } } cout << "You have chosen " << items << " items for a total of $" << total << ".\n"; return EXIT_SUCCESS; } void machine(double p) { items++; total += p; }