This seems to work better for me:
Code:
#include <iostream>
#include <cstdlib>
using namespace std;
int main ()
{
int numA = 5;
int numB = 5;
int answer = numA + numB;
int guess = 0;
for ( int attempts = 1; attempts<=3 && guess !=answer ; attempts++ )
{
cout << "What is " << numA << " + " << numB << "? ";
cin >> guess;
if (guess == answer)
{
cout << "Well done, got it in: " << attempts << " attempts!\n";
}
else
{
if ( attempts < 3 )
{
cout << "Incorrect. Please try again.\n";
}
else
{
cout << "Sorry, run out of attempts! Game over...";
}
}
}
}
Got rid of the breaks and seems to function ok.
Still not quite got my head around how to get a random number between 1 and 10 other than rand()%10 which in a lot of forums is not really a good way to do this.
Also, wanted to add a check whether a number has actually been entered, and also to give the user another go with y/n after they get it right.
Sam.