How do I get the variable total to get each price every time the program loops? The expected values is the total for every number input added together and each number has a specific value. I don't get any output when the number 8 is input, which is supposed to print and end the loop.

#include <iostream> using namespace std; int main() { int option; double one = 17.50; double two = 12.95; double thre = 11.56; double four = 7.99; double five = 15.00; double six = 9.99; double total = 0; do{ cout<<"**************************************BRAINS!**************************************"<<endl; cout<<"**************************************Menu**************************************"<<endl; cout<<"1) Thalamustard-Crusted Filet - $17.50"<<endl; cout<<"2) Hypothamoussaka - $12.95"<<endl; cout<<"3) Amyg-DAL-a - $11.56"<<endl; cout<<"4) CHIPpocampus & Dip- $7.99"<<endl; cout<<"5) Pons Pasta Special - $15.00"<<endl; cout<<"6) Medulla Frittata - $9.99"<<endl; cout<<"7) Finish Current Sale + Start a New Sale"<<endl; cout<<"8) Quit Forever - APOCALYPSE"<<endl; cin>>option;} while ( option <= 8 && option > 0); while(option <= 0){ if (option == 1){ double total = total + one; } else if (option == 2){ double total = total + two; } else if (option == 3){ double total = total + thre; } else if (option == 4){ double total = total + four; } else if (option == 5){ double total = total + five; } else if (option == 6){ double total = total + six; } else if (option == 7){ double total = total + (total*0.06); } else{ cout<<"HURRY UP!"; } if (option == 8){ cout<<"Your total is: $"<<total; //This does not print or accumulate } } } 
7

2 Answers

Every time you are creating a new local total variable in the lines

 double total = total + one; 

and it is not changing the actual total value. Changing this line to

 total += one; //or two.... 

can solve this issue.

Why your Code not working

look at the code equivalent

 do{ // print measseges cin>>option; } while (option <= 8 && option > 0); while(option<=0){ // decode the option meaning } 

the code will circular in the first loop (printing msg and get an input) and exit the loop when the option variable greater then 8 or a nigitive value

  • if first loop end with option is positve (greater then 8) then the second loop will never excuited and the program will termenate without any response !!

  • the second loop will only excuit when option varible is negative value ... and in this case , that will make an infinity loop >>> only the else statment will excuite for ever.

so you have two main error here

  1. the option in the second loop (if first loop termentate with negative value)is always negative so non of your if statment check has meaning
  2. you declare Total variable multible times

and those problem could be solved by the follwing code :

#include <iostream> using namespace std; int main() { int option; double one = 17.50; double two = 12.95; double thre = 11.56; double four = 7.99; double five = 15.00; double six = 9.99; double total = 0; do { cout << "**************************************BRAINS!**************************************" << endl; cout << "**************************************Menu**************************************" << endl; cout << "1) Thalamustard-Crusted Filet - $17.50" << endl; cout << "2) Hypothamoussaka - $12.95" << endl; cout << "3) Amyg-DAL-a - $11.56" << endl; cout << "4) CHIPpocampus & Dip- $7.99" << endl; cout << "5) Pons Pasta Special - $15.00" << endl; cout << "6) Medulla Frittata - $9.99" << endl; cout << "7) Finish Current Sale + Start a New Sale" << endl; cout << "8) Quit Forever - APOCALYPSE" << endl; cin >> option; if (option == 1) { total = total + one; } else if (option == 2) { total = total + two; } else if (option == 3) { total = total + thre; } else if (option == 4) { total = total + four; } else if (option == 5) { total = total + five; } else if (option == 6) { total = total + six; } else if (option == 7) { total = total + (total * 0.06); } else if (option == 8) { cout << "Your total is: $" << total; breack; } else { cout << "not valied input"; } } while (option <= 8 && option > 0); } 

code tips for better Reading

  1. you could encapsulate the printing menue in another function
  2. use switch statment rather than if it will make it much more cleaner to read
  3. do while is suck use while loop with true condition at first
  4. you coude use array rather than discret variable for saving constant

look for the next code it much more beautiful after apply this simple tricks

#include <iostream> using namespace std; void printMenu(); int main() { int option =0; double one = 17.50; double two = 12.95; double thre = 11.56; double four = 7.99; double five = 15.00; double six = 9.99; double total = 0; cout << "**************************************BRAINS!**************************************" << endl; while(option != 8) { printMenu(); cin >> option; switch (option) { case 1:total += one;break; case 2:total += two;break; case 3:total += thre;break; case 4:total += four;break; case 5:total += five;break; case 6:total += six;break; case 7: cout << "Your total is" << total << " $\n starting new sale ... \n"; total =0; break; case 8: cout << "Your total is: " << total << " $ \n"<<"End of the program"; break; default: cout << "you entered "<<option<<"and this is not a valid option please try agian\n"; break; } } } void printMenu() { cout << "**************************************Menu**************************************" << endl; cout << "1) Thalamustard-Crusted Filet - $17.50" << endl; cout << "2) Hypothamoussaka - $12.95" << endl; cout << "3) Amyg-DAL-a - $11.56" << endl; cout << "4) CHIPpocampus & Dip- $7.99" << endl; cout << "5) Pons Pasta Special - $15.00" << endl; cout << "6) Medulla Frittata - $9.99" << endl; cout << "7) Finish Current Sale + Start a New Sale" << endl; cout << "8) Quit Forever - APOCALYPSE" << endl; } 
2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.