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

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    81

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

    After much effort, I've gotten to here. This tells a user to re-guess if outside range, but does not react to a number within the correct range, nor the correct number.

    I know its probably full of things that could be tidier, but we're having severe problems with our course. Most of the students are feeling like we're sinking and we're trying to work out ways to rectify.

    Please could someone help, in the simplest terms possible because my brain is toast at this stage, how to finally finish this off?

    Code:
    #include <stdio.h> #include <stdlib.h>
    #include <math.h>
    #include <conio.h>
    #define ANSWER 4 /*Define constant of ANSWER*/
    int main(void)
    {
    	int guess;
    
    
    	printf("Enter your guess. Number is between 1 and 10:");
    	scanf("%d", &guess);
    
    
    
    
    	while( guess >= 1 || guess <= 10)
    	{
    
    
    		if(guess > 10)
    		{
    		printf("Too high, Guess again:");
    		scanf("%d", &guess);
    		}
    
    
    		if(guess < 1)
    		{
    		printf("Too low, guess again:");
    		scanf("%d", &guess);
    		}
    
    
    	}
    
    
    	while( guess <= 1 || guess >= 10)
    
    
    	{
    
    
    		if ( guess == ANSWER ) /*If clause to define if user's guess is correct*/
    		{
    		printf("You guessed correctly, the answer was 4."); /*If guess is correct this message will show*/
    		}
    
    
    		if (guess < ANSWER) /*If clause to define if user's guess is smaller than ANSWER*/
    		{
    		printf("You guessed incorrectly, the answer is greater."); /*If guess is smaller this message will show.*/
    		}
    
    
    		if (guess > ANSWER) /*If clause to to define if user's guess is greater than ANSWER*/
    		{
    		printf("You guessed incorrectly, the answer is smaller."); /*If guess is smaller then this message will show */
    		}
    
    
    	}
    return 0;
    }
    Last edited by Interista; 10-20-2011 at 09:10 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Please indent your code properly. It will make the logic much easier to follow.
    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
    Registered User
    Join Date
    Oct 2011
    Posts
    81
    Quote Originally Posted by laserlight View Post
    Please indent your code properly. It will make the logic much easier to follow.
    Logic? Seriously? Illogical I think. A computer saying I won't do what you want cos you didn't ask in EXACTLY the right way?
    Last edited by Interista; 10-20-2011 at 09:12 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Let's examine this loop:
    Code:
    int guess;
    
    printf("Enter your guess. Number is between 1 and 10:");
    scanf("%d", &guess);
    
    while (guess >= 1 || guess <= 10)
    {
        if (guess > 10)
        {
            printf("Too high, Guess again:");
            scanf("%d", &guess);
        }
    
        if (guess < 1)
        {
            printf("Too low, guess again:");
            scanf("%d", &guess);
        }
    }
    Suppose the user enters 5. 5 >= 1, so control enters the loop body. But wait, you don't want that to happen! So change the loop condition to terminate when the user enters input within the range.
    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

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    81
    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.
    Last edited by Interista; 10-20-2011 at 09:28 AM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Interista
    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.
    A while loop keeps looping while its condition evaluates to true.

    EDIT:
    Quote Originally Posted by Interista
    Logic? Seriously? Illogical I think. A computer saying I won't do what you want cos you didn't ask in EXACTLY the right way?
    Your C compiler does not care about indentation. I care about indentation, because "it will make the logic much easier to follow". I am obviously a far better C programmer than you, so perhaps you should care about indentation too
    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

  7. #7

  8. #8
    Registered User
    Join Date
    Oct 2011
    Posts
    81
    Quote Originally Posted by laserlight View Post
    A while loop keeps looping while its condition evaluates to true.

    EDIT:

    Your C compiler does not care about indentation. I care about indentation, because "it will make the logic much easier to follow". I am obviously a far better C programmer than you, so perhaps you should care about indentation too
    Why so arrogant? I'm pretty sure I speak better Italian than you. Do I get a medal?

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by Interista View Post
    Why so arrogant? I'm pretty sure I speak better Italian than you. Do I get a medal?
    That is NOT arrogant; that is reality laserlight is one of best, if not the best, programmer that posts on this site.

    Tim S.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Interista
    Why so arrogant? I'm pretty sure I speak better Italian than you. Do I get a medal?
    I gave you good advice with a reason for it, but you called it "illogical", thereby stating that I was being illogical. Arrogant, no?

    Now I point out that this advice carries the weight of my experience with it, you call me arrogant. If you don't want to be helped, then I won't help you. Carry on complaining about your teachers, for all the good it will do you.

    Quote Originally Posted by Interista
    I'm pretty sure I speak better Italian than you. Do I get a medal?
    I am not learning Italian; you are learning C programming.
    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

  11. #11
    Registered User
    Join Date
    Oct 2011
    Posts
    81
    Look, perhaps I misinterpreted what you meant. I took it as a slap-down that I hadn't indented. I'm putting hours into this ******* course and I'm pretty much sick of it. If you meant it as a slap-down then I stand by what I said, if not, and it seems you meant it genuinely, then I apologise.

    Don't belittle Italian though, I think you'll find its pretty useful.
    Last edited by Interista; 10-20-2011 at 10:11 AM.

  12. #12
    Registered User joybanerjee39's Avatar
    Join Date
    Oct 2011
    Location
    kolkata
    Posts
    106
    indentation is a very basic thing about programming . it helps every programmer who is reading the program afterwards to understand the logic better . even after few days when you will see your program you will find it easier to understand if you use indentation. that is basically what laserlight had to say.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Interista
    I was attacking programming rather than Laserlight. I had no quip with Laserlight.
    Fair enough.

    Quote Originally Posted by Interista
    I do have a quip with putting hours into an assignment and getting nowhere with it because a computer refuses to accept anything I write.
    You could read the thread on a development process. The main point: make a change, compile and test. Make another change, compile and test. Don't make many changes at one go, because it will be harder to spot what change introduced an error (not just a compile error, which can be easy to fix with line numbers in the compile error: it could be a logic error too).

    Quote Originally Posted by Interista
    People are logical, computers illogical.
    If computers are illogical, it is because people designed, manufactured and/or programmed them that way

    Quote Originally Posted by Interista
    And when an "expert" kicks off on you because you made a small error, when you've already said you're completely confused, its hard to take that you've worked hours at something and someone tells you what you've done is a load of ****.
    Who said that what you've done is "a load of ****"?

    Quote Originally Posted by Interista
    And there's no shame in being able to speak a language pally, I'd say more people understand Italian than this code stuff.
    Who said it was shameful to be able to speak a language?
    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

  14. #14
    Registered User
    Join Date
    Oct 2011
    Posts
    81
    Thanks all. Look I'm not at my best at the moment. I'm probably searching for an outlet for my frustration over the course and I took it out on the people trying to help me.

    Sorry.

    OT Ironically enough an Italian salesman just called to my house looking to sell me broadband, had a good conversation about football lol.

  15. #15
    Registered User
    Join Date
    Oct 2011
    Posts
    81
    And I fixed the problem with five ifs, so maybe my tantrum paid off cos I started thinking of other things than the program.

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