Hi, I am having issues with simulating a vending machine that asks for nickels, dimes, quarters and dollars bills. First of all, the program is not calculating the total (though it was before) so it doesn't run through all the conditions. If you could help me with the loop and formula I would appreciate it.
#include <iostream>
using namespace std;
int main()
{
do{
int nickel=0;
int dime=0;
int quarter=0;
int dollar=0;
int total=0;
cout << "Insert a coin or dollar\n";
cout << "How many nickels?\n";
cin >> nickel;
cout <<total;
cout << "How many dimes?\n";
cin >> dime;
cout <<total;
cout << "How many quarters?\n";
cin >> quarter;
cout <<total;
cout << "How many dollars?\n";
cin >> dollar;
cout << "Your total is\n";
total = (nickel * 5) + (dime * 10) + (quarter *25) + (dollar*100);
cout <<total<<endl;
}while (total < 350);
if(total==350){
cout<<"Here is your candy\n";
}
else{
int change=total-350;
cout<<"Here is your change\n";
}
What does the program print and what do you expect to be printed instead? Always provide this information (together with the input you gave the program) for future questions.
Try moving the variable definitions and initializations of the nickel, dime, etc. out of the do while loop. You are setting them back to zero with each interation.