//this program runs fine but i struggle to let the user enter a guess number 5 times. Basically a user needs to enter two large numbers and then guess 5 times for the number if the user reach number of guess it must say check line sorry better luck next ime.
#include <iostream>
using namespace std;
int main()
{
int num1, num2;
int sum;
int userGuess;
const int MAXGUESS = 5;
int noOfGuesses;
bool userCorrect;
noOfGuesses = 0;
userCorrect = false;
cout<< "Enter two large interger values "<<endl;
cin>>num1 >>num2;
sum = num1 + num2;
if (noOfGuesses <= MAXGUESS)
{
cout<<"Please Guess "<<endl;
cin>>noOfGuesses;
}
else if (noOfGuesses <= MAXGUESS)
{
cin>>noOfGuesses;
}
//else if (
// if (userCorrect == 1)
// {
cout<<"Sorry better luck net time! "<<endl;
cout<<"The sum was "<< sum;
return 0;
}
please use the code tag when typing code
[ code]your code[ /code]
and you did not define the problem you're having..
might i assume the loop runs more than 5 times?
if so remember counting starts at 0 in programming. you initialized noOfGuesses to 0 then in the statement if (noOfGuesses <= MAXGUESS) you say <= 5.. that is 0, 1, 2, 3, 4, 5 then that is (6) times. omit the < and try again.
You need to use a loop of some sort to repeat a set of statements. A for loop with an exit case would work:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
for (int i = 0; i < MAXGUESS; i++)
{
cout<<"Please Guess "<<endl;
cin>>userGuess; // wrong variable in your code, I have corrected it
if (userGuess == sum) //you need to check their guess against the sum
{
cout<<"You guessed correctly! "<<endl;
break; //if they guessed correctly, break the loop, no more guesses
}
elseif (i == MAXGUESS - 1) //if this is the last guess, end the game
{
cout<<"Sorry better luck next time! "<<endl;
cout<<"The sum was "<< sum;
}
}