title: Random Numbers and programming logic
body: <p>I'm having trouble with the assignment below.</p>
  
  <p>"Write a program that generates a random integer between 1-100 and then asks the user to guess what the number is. If the user's guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the program should display “Too low, try again.” The program should use a loop that repeats until the user correctly guesses the random number."</p>
  
  <p>How do I make the random numbers work, and is there a better/more efficient way to write any parts? I'm still learning C++</p>
  
  <p>Here's my code</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;ctime&gt;
  #include &lt;cstdlib&gt;
  using namespace std;
  
  int main()
  {
      // declare variables
      int rightAnswer, userAnswer;
  
      // determine rightAnswer
      srand (time(NULL));
      rightAnswer = (rand() % 100) + 1;
  
      // begin the game
      cout &lt;&lt; "I'm thinking of a number between 1-100!" &lt;&lt; endl;
      do{
          // collect data
          cout &lt;&lt; "Guess: ";
          cin &gt;&gt; userAnswer;
  
          // if else statements to determine correctness
          if (userAnswer &lt; 1 || userAnswer &gt; 100)
              cout &lt;&lt; "The number is in the range 1-100. Try again!" &lt;&lt; endl;
          else if (userAnswer &gt; rightAnswer)
              cout &lt;&lt; "Too high! Try again!" &lt;&lt; endl;
          else if (userAnswer &lt; rightAnswer)
              cout &lt;&lt; "Too low! Try again!" &lt;&lt; endl;
          else
              cout &lt;&lt; "That's it! Good job!" &lt;&lt; endl &lt;&lt; ":)";
  
  
      } while (userAnswer != rightAnswer);
      return 0;
  }
  </code></pre>