Question

I have a quick question about a glitch in this coding.

When I ran this program and it entered this loop (by inputting 1 as the number), everything went smoothly, except that even though I entered ++generalcounter1; into the body of the loop, it doesn't appear to be adding one to this variable every time the loop plays through. It never runs the message it is supposed to run when the variable == 10.

Please help me I'm very frustrated! Your advice is greatly appreciated!

Sorry if this was unclear, I'm new to this site as well as programming.

while (number == 1){
 int addend1;
 int addend2;
 int sum;
 int answer1;
 int generalcounter1 = 0;
 int reply1;

 ++generalcounter1;

 addend1 = 1 + rand() % 50;
 addend2 = 1 + rand() % 50;

 sum = (addend1 + addend2);

 cout << "\nWhat is " << addend1 << " plus " << addend2 << "? ";
 cin >> answer1;

 if (answer1 == sum){
     reply1 = winstatement(); 
 }          
 else{
     reply1 = losestatement();
 }

 if (generalcounter1 == 10){

     if (rightanswers >= 8){
        cout << "\nCongratulations, you are ready to go to the next level!\n" << endl;
     }
     else{
        cout << "\nPlease ask your teacher for extra help.\n" << endl;
     }  
  }   
}
Was it helpful?

Solution

You're declaring a new generalcounter1 each iteration of the loop, incrementing it, and having it be destroyed at the end of the iteration. Move the declaration outside of the loop to have just the one incrementing each time, instead of a new one each time that just gets destroyed before it's ever incremented twice.

int generalcounter1 = 0;
while (number == 1) { 
    //loop body
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top