Thread: Number Guessing

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    3

    Number Guessing

    First post here, hopefully more to come. Anyway, I'm in an intro to C programming course and about in my fourth week in. So far, we've gone over printf/scanf functions, for/while loops, if statements and stuff like that.

    Anyway, my teacher's powerpoint slides don't match the ones he had in class yet so I'm having a hard time with our the homework assignment. It's a number guessing program. What we have to do is get two user selected numbers, a lower number and higher number of any range. Then the program has to check if the first number is actually lower than the second number. If not, it tells the user to start over. After that, the computer randomly chooses a number between the lower and higher number (range). The user than has to guess the number. Finally, it has to end when the number guessed is correct and then output all the numbers with the correct number showing "correct" next to it. The program should ask the user if he/she wants to play again.

    I am willing to learn, so here is the code I have now. I know there are syntax errors, but I was working on getting the main skeleton down (probably a bad habit). Can someone help push me in the right direction. I have a feeling I want to utilize a while loop and I know I need a sentinel in there somewhere for the last part. Keep in mind, it is pretty incomplete, but how far off am I? I wanted to check now so debugging will be easier, hope that made sense. Thanks in advance.

    Code:
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    
    int main()
    
    int seed;
    int counter;
    int number_1;
    int number_2;
    int my_random;
    int guess;
    
    seed=time(NULL);
    srand(seed);
    
    {
    	printf("I will think of a number between two numbers of your choice. \n");
    	printf("What is the lower number?");
    	scanf("&#37;d", &number_1);
    	printf("What is the higher number?");
    	scanf("%d", &number_2);
    	
        while(number_1>=number_2)
        {
         printf("The first number must be lower than the second.  Please try again. \n");
         }
        else(number_1<=number_2)
        {
         printf("I have figured out my number. Start guessing by entering a number.");
         my_random==(rand()>number_1<number_2;
         scanf("%d", &guess);
         }
         if(guess==my_random)
         {
           printf("You are correct!");
         }
         else(guess!=my_random)
         {
           printf("Wrong! Try again.");
           }
      printf("Would you like to play again?")
    }
    EDIT: so, to summarize: Is my first boolean statement fine with rand being set to the range between 1 and 2? If someone can point out how to make it within a while statement ended with a sentinel, I think I'm almost there. argh, I'm having more trouble on this assignment than my last calculus one.
    Last edited by blacknapalm; 09-30-2008 at 06:02 PM.

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    You need to have the entire thing enclosed within a while(), and break out of that when the user doesn't want to play any more.

    You have to put in a correct formula to make my_random be between number_1 and number_2. Look up what range of values rand() generates and scale that to be (number_2 - number_1)+1 or something like that. Depending on whether you expect the guesses to include the numbers themselves. Do "hand" calculations on the lowest number and highest number rand() will ever give you so make sure the calculation gives the proper range.

  3. #3
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by blacknapalm View Post
    I was working on getting the main skeleton down (probably a bad habit).
    Actually that's a good way of going about it. Even better is to write it out in pseudo code first. Then you just have to translate pseudo code into your skeleton and then flesh (pardon the pun) the whole thing out.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 02-19-2009, 07:19 PM
  2. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  3. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  4. My number guessing game
    By The Gweech in forum C++ Programming
    Replies: 7
    Last Post: 06-22-2002, 09:03 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