Reputation: 217
cout<<"========================================="<<endl<<endl;
cout<<"The amount you need to pay is RM "<<total<<endl;
cout<<"=========================================="<<endl<<endl;
cout<<"You can pay using ($0.10 [1] $0.20 [2] $0.50 [3] $1 [4] $5 [5] $10 [6] $20 [7] $50 [8] )"<<endl;
cin>>choice2;
switch(choice2){
case 1:
total = total - 0.10;
break;
case 2:
total = total - 0.20;
break;
case 3:
total = total - 0.50;
break;
case 4:
total = total - 1;
break;
case 5:
total = total - 5;
break;
case 6:
total = total - 10;
break;
case 7:
total = total - 20;
break;
case 8:
total = total - 50;
break;
default:
cout<<"inavalid!"<<endl;
}
if(total > 0){
cout<<"you still need to pay "<<total<<endl;
cin>>choice2;
}
Extra information: My total is $5
I am trying to let it loop until the total amount is paid, say I choose case 4 which is $1. It's suppose to let me insert the remaining amount I am supposed to pay, which is $4, but the program ends after I insert another switch case option.
This part, isn't it supposed to be looping until my total is 0?
if(total > 0){
cout<<"you still need to pay "<<total<<endl;
cin>>choice2;
}
Thanks in advance for any help, I am also happy to learn any shorter way of writing this program if there is any, also is there anyway I can implement array into this program?
Upvotes: 0
Views: 70
Reputation: 82
No, neither the switch
nor the if
will cause your program to loop.
You're probably looking for something along the lines of
while(total > 0)
{
cin>>choice2;
switch(choice2){
// left out for clarity
}
if(total > 0){
cout<<"you still need to pay "<<total<<endl;
//Instead of getting the input in 2 different locations, just get it again at the start of the next loop.
}
}
Upvotes: 1