generate random number and compare to user input

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fatimahtaher
    New Member
    • Jan 2007
    • 11

    generate random number and compare to user input

    Hi,

    I am supposed to create a program that generates a random number and then asks the user to guess the number (1-100). The program tells the user if he guessed too high or too low. If he guessed right, it asks the user is he/she wants to play again. If the answer is yes, it generates a random number and asks the user to guess the number again. The user can exit if he enters 0. I have created the following code so far but it does not work. I would appreciate any help.



    Code:
    static void Main(string[] args)
          {
             
             Random randomNumbers = new Random();
             int secretNumber = randomNumbers.Next(1,101);
             double enterNum = 0;
    
             do
             {
                
                        
                // pick random number from 1 to 100
                
                Console.Write("{0} ", secretNumber);
    
                //get input
                Console.WriteLine("Please enter a number from 1 to 100 (O to quit)", enterNum);
                enterNum = Convert.ToDouble(Console.ReadLine());
    
             //loop when the generated number is not equal to the number entered by user
             }
             while (secretNumber != enterNum)
             {
    
                   while (enterNum != 0)
                   
                      //if the random number is less that the number entered by user
                      if   (secretNumber > enterNum)
                      Console.WriteLine("Your guess is too low.", enterNum);
    
                      //if the random number is more than the number entered by the user
                      else if (secretNumber < enterNum)
                      Console.WriteLine("Your guess is too high.", enterNum);
    
                   
             }
              
              
                //if the random number is equal to the number entered by user
                if (secretNumber == enterNum)
                {
                   Console.WriteLine(" You got it right.");
    
                   do
                   {
                      //ask to play again
                      string decision;
    
                      Console.Write("Do you want to play again (yes/no)? ");
                      decision = Console.ReadLine();
                   }
    
                   while (decision != "yes")
                   {
                      // Create random number generator
                      Random randomNumbers = new Random();
    
                      //declare variable to store random number
                      int secretNumber;
                      double enterNum;
    
    
                      //initialize variables
                      enterNum = 0;
    
    
                      // pick random number from 1 to 100
                      secretNumber = randomNumbers.Next(1, 101);
                      Console.Write("{0} ", secretNumber);
    
                      //get input
                      Console.WriteLine("Please enter a number from 1 to 100 (O to quit)",enterNum);
                      enterNum = Convert.ToDouble(Console.ReadLine());
    
                   }
                }
    
             Console.WriteLine("Thank you for playing.");
    
    
             }
          }
       }
  • Motoma
    Recognized Expert Specialist
    • Jan 2007
    • 3236

    #2
    This looks like C# code, am I correct?
    What doesn't work about this code?
    Are you getting any compiler errors?

    And next time, please post all of your code in [CODE ] [/CODE ] brackets!

    Comment

    • fatimahtaher
      New Member
      • Jan 2007
      • 11

      #3
      Yes, it is C#. It does not like my nest while loops. It thinks it is a statement and expects me to put a semicolon at the end of the while statement.

      Sorry about the code not being in the code brackets. Didn't know I had to do that.

      Originally posted by Motoma
      This looks like C# code, am I correct?
      What doesn't work about this code?
      Are you getting any compiler errors?

      And next time, please post all of your code in [CODE ] [/CODE ] brackets!

      Comment

      • willakawill
        Top Contributor
        • Oct 2006
        • 1646

        #4
        Originally posted by fatimahtaher
        Yes, it is C#. It does not like my nest while loops. It thinks it is a statement and expects me to put a semicolon at the end of the while statement.

        Sorry about the code not being in the code brackets. Didn't know I had to do that.
        Yes I can see that it would reject this code.
        The first while statement is being read as the end of the do_while pair so the compiler wants to see a semicolon
        Code:
        do
                 {
                    
                            
                    // pick random number from 1 to 100
                    
                    Console.Write("{0} ", secretNumber);
        
                    //get input
                    Console.WriteLine("Please enter a number from 1 to 100 (O to quit)", enterNum);
                    enterNum = Convert.ToDouble(Console.ReadLine());
        
                 //loop when the generated number is not equal to the number entered by user
                 }
                 while (secretNumber != enterNum)
                 {
        
                       while (enterNum != 0)
        This is also the case for the next do statement
        Code:
        do
                       {
                          //ask to play again
                          string decision;
        
                          Console.Write("Do you want to play again (yes/no)? ");
                          decision = Console.ReadLine();
                       }
        
                       while (decision != "yes")
                       {
        The compiler is expecting a matching while statement which should terminate with a semicolon

        Comment

        • fatimahtaher
          New Member
          • Jan 2007
          • 11

          #5
          How do I fix it? Have been working on it all week and I have lost all objectivity.

          Originally posted by willakawill
          Yes I can see that it would reject this code.
          The first while statement is being read as the end of the do_while pair so the compiler wants to see a semicolon
          Code:
          do
                   {
                      
                              
                      // pick random number from 1 to 100
                      
                      Console.Write("{0} ", secretNumber);
          
                      //get input
                      Console.WriteLine("Please enter a number from 1 to 100 (O to quit)", enterNum);
                      enterNum = Convert.ToDouble(Console.ReadLine());
          
                   //loop when the generated number is not equal to the number entered by user
                   }
                   while (secretNumber != enterNum)
                   {
          
                         while (enterNum != 0)
          This is also the case for the next do statement
          Code:
          do
                         {
                            //ask to play again
                            string decision;
          
                            Console.Write("Do you want to play again (yes/no)? ");
                            decision = Console.ReadLine();
                         }
          
                         while (decision != "yes")
                         {
          The compiler is expecting a matching while statement which should terminate with a semicolon

          Comment

          Working...