Thread: Random number generator game problem

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    11

    Random number generator game problem

    this is the first part of a program i have to make for a class and i need help with the guessing game part.
    when i type in a number to guess what number the computer has, it always says that i've guessed the right number
    here is the code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    main()
    {
     int x, y;
     int z = 0;
     printf("Welcome!\n");
     printf("Please select a game to play by typing in the correct number.\n");
     printf("1. Play the number guessing game between 1 and 50\n");
     printf("2. Play Rock, Paper, Scissors\n");
     printf("3. Display the Area of a rectangle\n");
     printf("4. Quit\n");
     scanf("%d\n", &x);
      if (x = 1){
    
    
       int stime;
       long ltime;
       int randomnumber = rand() % 51;
       ltime = time(NULL);
       stime = (unsigned) ltime/2;
       srand(stime);
    
    
       printf("Please enter a number between 1 and 50.\n");
       scanf("%d\n", &y);
       while (z < 1){
        if (y = randomnumber){
          printf("Congratulations! you guessed the correct number. Now exiting.\n");
    
    
          z++;
        }
        else if (y < randomnumber){
          printf("Guess higher\n");
          scanf("%d\n" , &y);
    
    
        }
        else if (y > randomnumber){
          printf("Guess lower\n");
          scanf("%d\n" , &y);
        }
       }
       exit(0);
    this is not the full program so somethings don't need to be brought up to me.
    the program has no errors, but the wording is wrong somewhere.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Right down the values of your variables. Then execute the loop by hand.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Sep 2013
    Posts
    11
    variable x is irrelevant to this part as it is to decide which game to go to
    variable y is the interger the user guesses
    variable randomnumber is the number the computer generates
    variable z is to terminate the program after the user has guessed correctly

    so the problem is with either 'y' or 'randomnumber'
    is it the order the if statements are in?
    hmmm

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    I'd bet that you're getting compiler warnings galore.

    Code:
    if (y = randomnumber){
    What do you think this line of code is doing?
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  5. #5
    Registered User
    Join Date
    Sep 2013
    Posts
    11
    well the plan is when the user inputs the number for y it will check to see if the number is the same as the random number that the computer guess

  6. #6
    Registered User
    Join Date
    Sep 2013
    Posts
    11
    also another issue is when trying to go to the second part of the program, it just goes to the guessing game.

  7. #7
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Quote Originally Posted by Quickstero View Post
    well the plan is when the user inputs the number for y it will check to see if the number is the same as the random number that the computer guess
    That's not what = does. The following two are equivalent:

    Code:
    if (y = randomnumber)
    Code:
    y = randomnumber
    if (y)
    You should be getting a compiler warning suggesting using a comparison operator (==) instead of the assignment operator (=).

    The == operator checks if the left and right hand sides are equal. The = operator sets the left hand side to the value of the right hand side.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  8. #8
    Registered User
    Join Date
    Sep 2013
    Posts
    11
    ah okay i remember that now let's see how this works

  9. #9
    Registered User
    Join Date
    Sep 2013
    Posts
    11
    alright now it wont get off of the screen where it says guess higher.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    main()
    {
     int x, y;
     int z = 0;
     printf("Welcome!\n");
     printf("Please select a game to play by typing in the correct number.\n");
     printf("1. Play the number guessing game between 1 and 50\n");
     printf("2. Play Rock, Paper, Scissors\n");
     printf("3. Display the Area of a rectangle\n");
     printf("4. Quit\n");
     scanf("%d\n", &x);
      if (x == 1){
         
       int stime;
       long ltime;
       int randomnumber = rand() % 51;
       ltime = time(NULL);
       stime = (unsigned) ltime/2;
       srand(stime);
       
       printf("Please enter a number between 1 and 50.\n");
       scanf("%d\n", &y);
       while (z < 1){
        if (y == randomnumber){
          printf("Congratulations! you guessed the correct number. Now exiting.\n");
       
          z++;
        }
        else if (y < randomnumber){
          printf("Guess higher\n");
          scanf("%d\n" , &y);
    
    
        }
        else if (y > randomnumber){
          printf("Guess lower\n");
          scanf("%d\n" , &y);
        }
       }
       exit(0);
      }

  10. #10
    Registered User
    Join Date
    Sep 2013
    Posts
    11
    why would it continue to tell the user to guess higher

  11. #11
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Get rid of the \n in all your scanf's.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  12. #12
    Registered User
    Join Date
    Sep 2013
    Posts
    11
    alright, lets try this out

  13. #13
    Registered User
    Join Date
    Sep 2013
    Posts
    11
    thanks so much now i can move onto the next one, which there will probably be another post later.

  14. #14
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    I should say more ....

    • Write "int main(void)" instead of just "main()".
    • Put a "return 0;" as the statement just before the final brace of main.
    • Move the srand call to just after "int z = 0;" and write it as "srand((unsigned)time(NULL));"
    • "rand() % 51" should be "rand() % 50 + 1".
    • Use more descriptive variable names.
    • Use functions.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  15. #15
    Registered User
    Join Date
    Sep 2013
    Posts
    11
    Alright last issue, the program will only generate the random number 10 and wont change
    update:
    never mind now it works.
    Last edited by Quickstero; 09-21-2013 at 06:52 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pseudo-random number generator problem
    By clarkk in forum C++ Programming
    Replies: 2
    Last Post: 06-27-2011, 09:31 AM
  2. need a random number generator thats not compleatly random
    By thedodgeruk in forum C++ Programming
    Replies: 1
    Last Post: 06-05-2011, 06:48 AM
  3. problem with random number generator
    By Niss in forum C Programming
    Replies: 6
    Last Post: 10-01-2008, 06:03 PM
  4. random number generator
    By noodle24 in forum C++ Programming
    Replies: 7
    Last Post: 05-11-2006, 10:41 AM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM