I am progressing on my "vending machine" code where users are able to add an unlimited amount of items to the cart until they press 0 to checkout. I ran into a problem with the array cost[item]
in my "do-while" loop where it says "subscripted value is not an array, pointer, or vector." Could someone please help me with this?
I also have a few smaller problems if someone would like to help. Here are my main problems I ran into:
the press 0 to checkout part of the code - I think this problem is related to the
cost[item]
array problem I described above.how to get my menu prices to display 2 digit decimals - I am not sure where to put
cout << fixed << setprecision(2) << total;
in my code if that is the correct way of doing it.how to print a statement when "checking out" to display items with total cost.
my full code:
#include <iostream>
#include <iomanip>
using namespace std;
string menuItems[5] = {"Popcorn", "Coconut Clusters" , "Granola Bar" , "Trail Mix" , "Chocolate"};
float cost[5] = {2, 3, 2.50, 1.50, 1};
void vendingMachine() {
for(int i = 0; i < 5; i++)
cout << i+1 << ". " << menuItems[i] << ": $" << cost[i] << endl;
}
int main() {
cout << "Vending Machine" << endl;
cout << "----Items------" << endl;
vendingMachine();
cout << "Enter 0 to checkout" << endl;
float cost;
int total;
total = 0;
do {
cout << "Enter your selection: " << flush;
int item;
cin >> item;
item = item -1;
cout << menuItems[item] << ": $" << cost[item] << " has been added to cart." << endl;
total = total + cost[item];
} while (item != 0);
cout << "Proceding to checkout..." << endl;
cout << "========================" << endl;
cout << "Amount due: " << total << endl;
cout << "Insert money here: $" << flush;
float money;
cin >> money;
if (money > cost) {
float change = money-cost;
cout << "Thank you! You have $" << change << " change." << endl;
}
if (money == cost) {
cout << "Thank you! Have a nice day!." << endl;
}
if (money < cost) {
float amountOwed = cost-money;
cout << "Please insert another $" << amountOwed << endl;
cout << "Enter amount: " << flush;
float payment;
cin >> payment;
if (payment > amountOwed) {
float change2 = payment-cost;
cout << "Thank you! You have $" << change2 << " change." << endl;
}
if (payment == amountOwed) {
cout << "Thank you! Have a nice day!." << endl;
}
if (payment < amountOwed) {
cout << "Sorry, you did not enter enough money. Your cart has emptied." << endl;
}
}
return 0;
}