Guess the number?

please anyone can help me to fix the program for playing guess number, because my program have a something problem that i don't know how to solve it.
The Question is :

A Guess number, player should guess a number from 1 until 100. then the program should display if the player input the number is 'too big' or 'too small'. it keeps continue until the player got the number. and the end we must show it 'how much times' that the player input the guess number.

the teacher told me to use 'while'.
Please help me.
thankyou before
1
2
3
4
5
6
7
8
9
10
variable number;

if guess < number
{...}

if guess == number
{..}

else // if guess > number
{...}


Also have a counter variable, keeping track of the number of guesses
And implement your while loop as well. If you are having trouble with the loop, try using the if/else statements on one case at first, until you have the basic idea of what you're doing.
i didn't get what you mean.. can you explain it more detail? and how to count how many times that we try to guess the number on it ?

thank you for helping me before
OK, this is not nearly a working code, it just shows the basic Idea/outline. I kind of used THEJJJUNK's previous code since it was a starting point, maybe not the way I would have personally started for readability, but oh well.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
int main()
{
int number = 34, count = 1, guess = 0, end = 0;
cout<<"Guess a number between 1 and 100, please only use whole numbers";
cin>> guess;
if (guess < number)
{..."you were a bit low, try again"}

else if (guess == number)
{... "congrats, you guessed correctly on the first try!"
"press another number to end the game"
cin>>end
return 0;
}

else // if guess > number
{... " you were a bit high, try again"}


while(guess != number)//this starts the loop that only ends if the number is guessed correctly
{
count ++ //the number of times the person has guessed

cin<<guess;

if (guess < number)
{
cout<< "Nope, "<< guess << " was a bit low, you have tried " << count << " times, try again \n";
}

else if (guess == number)
{.. "Congrats, you guessed correctly, and it only took you " << count << " tries!";
"press another number to end the game"
cin>>end
return 0;
}

else // if guess > number
{
cout<< "Nope, "<< guess << " was a bit high, you have tried " << count << " times, try again \n";
}

}

So what happens here is we run you basic number check once to see if they made the right choice first try through, if they did then you give them a message saying that. If they didn't then you can now enter the while loop and change the prompt to say that they guessed wrong and should try again. The variable 'count' keeps track of the number of times they have gone through the loop.


If you really want to impress the teacher, then run search on this site on how to generate a random number.



I just want to point out the reason why a correct guess requires the player to type in another number even if they won, some C++ compilers like windows C++ don't pause after you type in return 0; they just end the program so quickly that you don't have time to read the last cout, this way it is forced to write the message on the screen and pause for input before ending.
Last edited on
I also should point out that if you were to run the example code that THEJJJUNK originally posted it would run a little differently than what he expected.

The final else statement would only apply to the second if(), it wouldn't take into account the first if(), it just has to do with the way that the program prioritizes how it runs. Using if() else if() and then else will take in the argument as a whole.

Hope all of this helps Marvin77, I'm not sure how fluent your English is, hopefully you have a good translator website, not just google.
Last edited on
I also should point out that if you were to run the example code that THEJJJUNK originally posted it would run a little differently than what he expected.

You are right. I just typed that out as a basic example. I should have put else if.

Also, Instead of using the if, else if, else statements twice, you could use a do while loop instead.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

#include <iostream>
using namespace std;

int main()
{
   int number = 34, count = 0, guess = 0, end = 0;
   cout << "Guess a number between 1 and 100, please only use whole"
        << " numbers: ";

   do
   {
     ++count;

     cin >> guess;

     if (guess < number)
     {
       cout << "Nope, " << guess << " was a bit low, you have tried ";
       cout << count << " times, try again \n";
     }

     else if (guess == number)
     {
        cout << "Congrats, you guessed correctly, and it only took you "
                << count << " tries!\n";
        cout << "Press another number to end the game";
        cin >> end;

     return 0;
     }

     else // if guess > number
     {
     cout<< "Nope, "<< guess << " was a bit high, you have tried " 
         << count << " times, try again \n";
     }
   }  while(guess != number);// end do-while
}


I also initialized count=0 instead of count=1 to get the right count.
Last edited on
Topic archived. No new replies allowed.