Thread: Guess the Number.C problem

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    1

    Question Guess the Number.C problem

    Im a newbie programmer on C. And I have an error on my Guess the random number game. Can you please help me fix this? When you got the right answer it shows the "You got the right answer" and shows the "What is the number?"

    Here is the code.

    Code:
    #include<stdio.h>
    main()
    {
          srand(time(0));      
          int x=rand() % 101,guess=0,tries=3;
          printf("The computer will generate a random number, try and guess the random number.\n\n");
          printf("*Hint*\n");
          printf("It ranges only from 0-100\n");
          do{
                           printf("What is the number?%d",x); //I put the %d,x to know the random number for testing.
                            scanf("%d",&guess);
                                 if (guess>x){
                                              printf("You'r guess is higher than the number\n");
                                              tries--;
                                              }
                                              else if (guess<x){
                                                   printf("You'r guess is lower than the number\n");
                                                   tries--;
                                                   }
                                              else if (x=guess) 
                                              {
                                                    printf("\nYou Got the right answer!\n");
                                                    printf("The random number is %d",x);
                                        }
                                                                    
          }while(tries!=0);
                           if (tries==0){
                                        printf("\nYou have failed to guess the random number.\n");
                                        printf("The random number is %d",x);
                                        }
              getch();
              }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    For starters, you need to indent your code properly, e.g.,
    Code:
    #include<stdio.h>
    
    main()
    {
        srand(time(0));
        int x = rand() % 101, guess = 0, tries = 3;
        printf("The computer will generate a random number, try and guess the random number.\n\n");
        printf("*Hint*\n");
        printf("It ranges only from 0-100\n");
        do {
            printf("What is the number?%d",x); //I put the %d,x to know the random number for testing.
            scanf("%d", &guess);
            if (guess > x) {
                printf("You'r guess is higher than the number\n");
                tries--;
            }
            else if (guess < x) {
                printf("You'r guess is lower than the number\n");
                tries--;
            }
            else if (x = guess) {
                printf("\nYou Got the right answer!\n");
                printf("The random number is %d", x);
            }
        } while (tries != 0);
    
        if (tries == 0) {
            printf("\nYou have failed to guess the random number.\n");
            printf("The random number is %d", x);
        }
    
        getch();
    }
    Now, I notice that you have:
    Code:
    else if (x = guess) {
    You probably intended to use == instead of = here, but then you can just write:
    Code:
    else {
    since the first two conditions in the if-else chain already mean that x == guess by this point.

    Also, note that you should #include <stdlib.h> for srand, #include <time.h> for time, and explicitly declare main as returning an int.

    Furthermore, you should check the return value of scanf: what if the user entered some alphabetic text instead of the integer expected? Oh, and getch() is non-standard, but you probably don't need it here as long as you run your program from a command prompt.
    Last edited by laserlight; 08-09-2013 at 06:14 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You want to compare, not assign!!
    Code:
    else if (x==guess)
    Now, in order to break the loop, when "you got the right number" you could use the keyword break; or to use another variable called found, which will take the values 0 and 1.
    Outside of the loop you will initialize it with zero and then you will check if the value is 0. If it is and tries are not zero, then you can go on with the loop.
    If x == guess, then you got the right number, assign found the value 1, which will make the while condition fail.
    Think of what the while condition should be

    Welcome to the forum
    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

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    1. Work on your indentation
    2. line 20 - remove condition since it is wrong and not needed
    3. you need to exit from your program after number is guessed correctly to not
    a. ask again
    b. show the message on line 28/29
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 17
    Last Post: 10-20-2011, 06:32 PM
  2. Replies: 7
    Last Post: 10-19-2011, 08:45 AM
  3. Guess My Number (Need help)
    By dhardin in forum C++ Programming
    Replies: 5
    Last Post: 12-10-2009, 12:59 PM
  4. can u guess my number?
    By hotsox in forum C Programming
    Replies: 2
    Last Post: 01-07-2006, 01:40 PM
  5. can u guess my number?
    By strider496 in forum C Programming
    Replies: 16
    Last Post: 03-22-2005, 10:19 PM

Tags for this Thread