How Do I Get the Variable to Accumulate? C++

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);

   
}

Elena Rostova

Elena Rostova

Lead Health, Wellness & Medical Journalist

Elena Rostova holds a Master's degree in Public Health Journalism. She covers groundbreaking medical research, holistic wellness trends, mental health awareness, and nutritional science.