Discussion about math, puzzles, games and fun.   Useful symbols: ÷ × ½ √ ∞ ≠ ≤ ≥ ≈ ⇒ ± ∈ Δ θ ∴ ∑ ∫ • π ƒ -¹ ² ³ °

You are not logged in.

## Post a reply

Write your message and submit
|
Options

## Topic review (newest first)

ginstt
2011-09-24 02:18:00

I need to write a program where the user has to guess the random number that the program makes up. The range is between 0 - 100 and the user only gets 10 tries to guess the number. If the number is too high or too low, it informs the user by saying that the random number is lower or higher.

I'm still learning Java and C++ is very new to me, even though most of the stuff is similar. The program that I have written compiles and runs, but it doesn't seem to reach the steps that tell the user whether the number the entered is higher or lower than the random number. This is my first time using nested if's and I probably have them ordered in a way where they aren't being read.

Can anyone point me in the right direction so my program will follow through with the right step at the right time?

Here is my code:

#include &lt;iostream&gt;
#include &lt;time.h&gt;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

int main ()

int usersGuess;
int usersChances = 0;
int secretInt;

int MAX_VALUE = 100 + 1;
int MIN_VALUE = 0;

srand ( time(NULL) );

secretInt = rand() % 100 + 1;

while ( (usersGuess != secretInt) &amp;&amp; (usersChances &lt;= 9) )

cout &lt;&lt; &quot;The secret number is between 1 and 100.\n&quot;
&lt;&lt; &quot;What is your guess? &quot;;
cin &gt;&gt; usersGuess;

usersChances++;

if ( usersGuess &gt; MAX_VALUE &amp;&amp; usersGuess &gt; MIN_VALUE )

if ( usersGuess == secretInt )

cout &lt;&lt; &quot;Right! It took you &quot; &lt;&lt; usersChances &lt;&lt; &quot; guesses.&quot;;
break;

if ( usersGuess &lt; secretInt )

cout &lt;&lt; &quot;Too low, guess again? &quot;;
usersChances++;

if ( usersGuess &gt; secretInt )

cout &lt;&lt; &quot;Too high, guess again? &quot;;
usersChances++;

cout &lt;&lt; &quot;Invalid guess, guess again? &quot;;

Tips or suggestions are greatly appreciated. Thank you!