I need help when i run the app. and enter the wrong answer "No. please try again " continues to loop forever . I want it to say it only once and repeat the same question. Any ideas? I thought maybe the braces were off somewhere but I'm not sure.

//Exercise 12.16: MultiplicationTeacher.cpp
// This game asks the user to enter the product of two
// randomly generated numbers in the range 0-9.
#include <iostream> // required to perform C++ stream I/O
#include <ctime>    // contains prototype for function time
# include <iomanip>
// contains function prototypes for functions srand and rand
#include <cstdlib>

using namespace std; // for accessing C++ Standard Library members

// function prototypes
int generateQuestion();
void generateOutput();

// function main begins program execution
int main()
{
   // randomize random number generator using current time
   srand( time( 0 ) ); 

   // define variables
   int userAnswer = 0; // stores the user's answer
   int correctAnswer=0;  // stores the correct answer

while (userAnswer != -1 )
{
    {
    correctAnswer= generateQuestion ();
    cin >> userAnswer;
    }
while (userAnswer != correctAnswer && userAnswer != -1)

{
    cout << "\nNo. Please try again ( -1 to end ) ?"<<endl;
    }
    if(userAnswer==correctAnswer)
    {
        generateOutput();
    }

}
   return 0; // indicates successful termination

} // end function main

Edited 3 Years Ago by mike_2000_17: Fixed formatting

You're problem is you never give the user a chance to re-enter a new solution. After your while(userAnswer != correctAnswer && userAnswer != -1) you check if the solution is correct (and in the case where it's wrong) it will pop back up to the same while loop and print out the wrong answer statement, then go back up to the same while loop (Since the answer is still wrong since the user never gets a chance to re-enter it) and print out the wrong answer statement again. It will continue that forever. Try this:

while(userAnswer != -1)
{
   
  correctAnswer= generateQuestion ();
   while(userAnswer!=correctAnswer || usesrAnswer!=-1)
   {
      cin >> userAnswer;
      if(userAnswer==correctAnswer)
      {
         generateOutput();
      }
      else if(userAnswer!=-1)
          cout << "\nNo. Please try again ( -1 to end ) ?"<<endl;
      else
          cout<< "Thanks for playing, goodbye"<<endl;
    }
}

So here we first generate the question, then while the user's answer is not -1 or the correct one, we continue to ask for the user's input at the top of the loop. Each time we check and see if they are correct or if they entered -1. If either of those are true, the while loop is exited. If they were correct, then they still stay in the outer while loop (while(userAnswer!=-1). So the game continues until the user enters -1. I hope this helps, I typed it quickly so I apologize for any errors. Best of luck!

it does help me but if the user enters the correct answer I want it to generate another question and so on and so on until they -1 to exit

This question has already been answered. Start a new discussion instead.