#include #include #include #include using namespace std; int main() { struct item { string name; double price; }; vector shopping_list { {"Apples", 2.59}, {"Milk", 4.99}, {"Bread", 6.25} }; string input_name; double input_price; cout << "Current list has " << shopping_list.size() << " items.\n"; cout << "Would you like to add more? (Type 'done' for the name to stop)"; for (;;) { cout << "\nEnter item name: "; cin >> ws; //Discard the newline typed after the previous price. getline(cin, input_name); //input entire line, not just 1 word if (!cin) { cerr << "Sorry, couldn't read that.\n"; continue; //Go around the for loop again. } if (input_name == "done") { break; } cout << "Enter price: "; cin >> input_price; shopping_list.push_back({input_name, input_price}); } const vector::size_type n {shopping_list.size()}; double total_cost {0.0}; cout << "Final Grocery List" << "\n"; for (vector::size_type i {0}; i < n; ++i) { cout << shopping_list[i].name << ": $" << shopping_list[i].price << "\n"; total_cost += shopping_list[i].price; } cout << "Total Items: " << n << "\n"; cout << "Final Total: $" << total_cost << "\n"; return EXIT_SUCCESS; }