Thread: Program to prompt user to guess number and re-guess if outside range - almost there!

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Interista
    Sorry.
    Apology accepted

    Quote Originally Posted by Interista
    And I fixed the problem with five ifs, so maybe my tantrum paid off cos I started thinking of other things than the program.
    Yeah, taking a break can help. Anyway, you don't need 5 if statements in the loop body: the two that you had are fine. In fact, they are key to how you should write your loop condition. The logic should be something like this:
    Code:
    print request for guess
    read input for guess
    while guess is not in the range:
        if guess is greater than the upper bound of the range:
            print "too high"
        else if guess is less than the lower bound of the range:
            print "too low"
        read input for guess
    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

  2. #17
    Registered User
    Join Date
    Oct 2011
    Posts
    81
    Quote Originally Posted by laserlight View Post
    Apology accepted


    Yeah, taking a break can help. Anyway, you don't need 5 if statements in the loop body: the two that you had are fine. In fact, they are key to how you should write your loop condition. The logic should be something like this:
    Code:
    print request for guess
    read input for guess
    while guess is not in the range:
        if guess is greater than the upper bound of the range:
            print "too high"
        else if guess is less than the lower bound of the range:
            print "too low"
        read input for guess
    Problem is I've been at this assignment, which is supposed to take 10 hours max, for about 25 hours. I'm fed up. I really don't want to do anymore, and it could well be futile to do more anyway because if next week is as bad as this I'll be dropping off the course.

    I'm not a kid throwing my toys out the pram, I'm a lot older than a kid, and I've done enough courses on other things to know that things are not always rosy, and things need work. But the course is ridiculous, they give us about 1% of the information we need and expect us to work the things out ourselves.

    Which would be likely to get me higher marks, submitting the code I initially gave you which doesn't completely work or submitting the code with the five ifs that works but is too long. Honestly, I'm drawing a line on this assignment now, I can't do anymore or I'll lose my mind. My neck is sore, my fingers are sore, my brain hurts.

  3. #18
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Interista View Post
    Not being funny, but as I said above, we're all having huge problems with our course because its being taught badly. We're trying to get something done about it now. They told us how to use while, how to enter it, but not exit it. Hence you may as well ask me what's the Russian for elephant as I've as much chance of knowing.

    I tried switching the whiles around but it doesn't work. That's logic, but doesn't do anything.
    Exiting while is pretty simple... program flow (called "control") resumes at the closing brace when the condition is no longer met.

    For example...
    Code:
    #include <stdio.h>
    
    int main (void)
      {
         int x = 0;
    
         while ( x < 5 )
           { 
              printf("%d  ",x);
              x++; 
           }
         printf("Done!\n\n");
         
         return 0;
    }
    Type that up and run it... you'll see how it works.

    As for the course... we hear this complaint a lot, especially from India where they are turning out 1980s grade computer scientists, 20 years too late... It's good that you're trying to get something done about the poor teaching. But in the mean time you should grab a modern textbook or tutorial and study on your own.... anything is better than nothing, my friend.

    Now for your long running problem with this hi-lo game...
    Code:
    // high-low guessing game
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main (void)
      {  
         int secret, guess, turn;
    
         srand(time(NULL));
         secret = rand() %1000 + 1;
    
         printf("I have a number between 1 and 1000 (inclusive)\nYou have 10 tries to guess it...\n\n");
    
         turn = 1;
         while (turn < 11)
           {  
              printf("Turn %d : ",turn);
              scanf(" %d", &guess);
    
              if (guess < 1 || guess > 1000)
                {
                   printf("Between 1 and 1000 please\n");
                } 
             else if (guess < secret)
                {
                   printf("Too low\n");
                   turn ++;
                }
            else if (guess > secret)
                {
                   printf("Too High\n");
                   turn++;
                }
            else
                {
                   printf("\n\nWe have a winner!  The number was %d \n\n", secret);
                   exit(0);
                 }
            }
         printf("\n\nSorry, not this time!  The number was %d \n\n", secret);
         return 0;
    }

    You should not copy this code...
    it's now pubilished and your teacher may find it...
    but you should study it and see how it works.

    Good luck with your studies...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 10-19-2011, 08:45 AM
  2. Guess My Number (Need help)
    By dhardin in forum C++ Programming
    Replies: 5
    Last Post: 12-10-2009, 12:59 PM
  3. Guess the number program help needed
    By msbrownsugah in forum C Programming
    Replies: 7
    Last Post: 05-06-2009, 05:58 PM
  4. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  5. can u guess my number?
    By strider496 in forum C Programming
    Replies: 16
    Last Post: 03-22-2005, 10:19 PM