multiple entries

//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;
}
first --> Please use code tages ... Makes it easier to read.

Second --> to do things 5 times, use a for loop:

1
2
3
4
5
6

for(int i = 0 ; i < MAXGUESS ; i++ )
{
   // code placed here will be executed MAXGUESS  times
}
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.
Last edited on
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
   }
   else if (i == MAXGUESS - 1)  //if this is the last guess, end the game
   {
      cout<<"Sorry better luck next time! "<<endl;
      cout<<"The sum was "<< sum;
   }
}
Last edited on
thanks guys, i already took your advice to use code tags.




One of the problems may be that you're reading in "noOfGuesses" instead of "userGuess".

Think the above posts already show what you should change. If you've made changes, please post your updated code.
Topic archived. No new replies allowed.