| | |
Number guessing game problem
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2007
Posts: 3
Reputation:
Solved Threads: 0
I am very new to programming and am in a class that has quite the frustrating instructor. He gives very simple examples then asks very complex questions. Complex to me atleast. Anyone who could shed some light on these would be greatly appreciated.
Write a program to play a number guessing game with the user. The program should keep asking for a number between 1 and 100 in the input, comparing it with a stored value (e.g. "#define MY_NUMBER 72") and informing the user about the outcome, until the correct guess is made. Below is an example of a sample run, with user input in boldface.
Welcome to the game of Guess It!
I will choose a number between 1 and 100.
You will try to guess that number. If you guess wrong,
I will tell you if you guessed too high or too low.
Your guess: 67
Too low!
Your guess: 89
Too high!
Your guess: 0
Illegal guess. Your guess must be between 1 and 100.
Your guess: 72
*** CORRECT ***
Your score is 3.
(This is a modified Exercise 2.11 from the text. Note that there are some differences between the textbook version and this one - here the program should exit once the correct guess is made, it should not restrict the user to up to 6 guesses, and it should also keep track of the user's score, i.e. the number of guesses made until CORRECT, not counting illegal attempts.)
Internals: Your program should use a while loop in order to assure the continuing execution until the correct guess is made. Checking whether the user's input was correct should be done by a separate function (receiving the number provided in the input as its parameter, and returning 1 or 0, depending on whether the guess was correct or not), and checking whether the guess was correct should be done by another function (receiving the guess and the target as its parameters, and returning -1 for TOO_LOW, 0 for CORRECT and +1 for TOO_HIGH - all 3 values should be defined as pre-processor constants). The main block should implement the while loop, prompt the user for the input, check it and display the result.
Write a program to play a number guessing game with the user. The program should keep asking for a number between 1 and 100 in the input, comparing it with a stored value (e.g. "#define MY_NUMBER 72") and informing the user about the outcome, until the correct guess is made. Below is an example of a sample run, with user input in boldface.
Welcome to the game of Guess It!
I will choose a number between 1 and 100.
You will try to guess that number. If you guess wrong,
I will tell you if you guessed too high or too low.
Your guess: 67
Too low!
Your guess: 89
Too high!
Your guess: 0
Illegal guess. Your guess must be between 1 and 100.
Your guess: 72
*** CORRECT ***
Your score is 3.
(This is a modified Exercise 2.11 from the text. Note that there are some differences between the textbook version and this one - here the program should exit once the correct guess is made, it should not restrict the user to up to 6 guesses, and it should also keep track of the user's score, i.e. the number of guesses made until CORRECT, not counting illegal attempts.)
Internals: Your program should use a while loop in order to assure the continuing execution until the correct guess is made. Checking whether the user's input was correct should be done by a separate function (receiving the number provided in the input as its parameter, and returning 1 or 0, depending on whether the guess was correct or not), and checking whether the guess was correct should be done by another function (receiving the guess and the target as its parameters, and returning -1 for TOO_LOW, 0 for CORRECT and +1 for TOO_HIGH - all 3 values should be defined as pre-processor constants). The main block should implement the while loop, prompt the user for the input, check it and display the result.
From the problem description, it seems you should be pretty well into the course by now - loops and functions are requirements.
That last paragraph give you quite a bit of information on how you ought to structure the program. The two functions are really quite simple, doing some comparisons to validate and test the guess. Start with those.
Does the Ex 2.11 referenced give you any model you can follow? It looks like you can use that as a starting point.
Please try to solve this, then if you still have problems, post your code and point out where you feel your problems lie.
Val
That last paragraph give you quite a bit of information on how you ought to structure the program. The two functions are really quite simple, doing some comparisons to validate and test the guess. Start with those.
Does the Ex 2.11 referenced give you any model you can follow? It looks like you can use that as a starting point.
Please try to solve this, then if you still have problems, post your code and point out where you feel your problems lie.
Val
•
•
Join Date: Oct 2007
Posts: 3
Reputation:
Solved Threads: 0
This is only my second homework and it is from chapter 2. The example problem he claims he got from the book is incredibly simple compared to what I am suppose to do here. I have the main part down I am assuming but there are many finite details that I think I am leaving out. This is my first programming class ever had and I am COMPLETELY lost. The only things I really have down are the printf command and the scanf command. Ive come to forums online as a last resort. I have three other guys im working with in class that are just as lost as I am. I'm working on it and will post what I have to get some feedback.
Last edited by onlyled12; Oct 9th, 2007 at 1:44 am.
•
•
Join Date: Oct 2007
Posts: 3
Reputation:
Solved Threads: 0
Here is what I have so far.
That code does not work. I am having trouble getting it to keep track of the guesses for the score and also in trying to get it to display the "Illegal guess. Your guess must be between 1 and 100" part. The "else if ( guess > 100 )" and "else if (guess < 1 )" seem to be some kind of wrong command in that the 1 and 100 aren't recognized. This is all pretty hard for me and quite frustrating so once again, all help is welcome.
C++ Syntax (Toggle Plain Text)
#include <stdio.h> #define TARGET 17 #define LOW 0 #define HIGH 100 #define TRUE 1 #define FALSE 0 int process( int guess, int target ); int main( void ) { int a_guess, correct; correct = FALSE; printf( "Welcome to the game of Guess It!\n" ); printf( "I will choose a number between %d and %d.\n", LOW, HIGH ); printf( "You will try to guess that number. If you guess wrong,\n" ); printf( "I will tell you if you guessed too high or too low.\n" ); while ( correct == FALSE ) { printf( "Try to guess it now. " ); scanf( "%d", &a_guess ); correct = process( a_guess, TARGET ); } return 0; } int process( int guess, int target ) { if ( guess < target ) printf( "Too low!\n" ); else if ( guess > target ) printf( "Too high!\n" ); else if ( guess > 100 ) printf( "Illegal guess. Your guess must be between 1 and 100.\n" ) else if (guess < 1 ) printf( "Illegal guess. Your guess must be between 1 and 100.\n" ) else { printf( "You guessed it!\n" ); return TRUE; } return FALSE; }
Last edited by Ancient Dragon; Oct 9th, 2007 at 8:20 am. Reason: add code tags
First of all, is this a course in C or C++? If in C, you might be better off using the C forum here.
In any case, the method of solving your problem will be essentially the same.
Reread the instructions. Your function is doing too much. First, it's doing two jobs (validating guess and testing if guess is correct.) Secondly, it's doing more than is asked, in that you have the function giving the output to the user. Each function should be very much like the mathematical idea of a function - some value(s) go in, a value comes out. In both cases, the functions should be returning an int based on the relationship of guess to the valid range, or guess relative to the secret number. Both these functions need only be a couple lines long.
You're on the right track with your loop in main( ), but the output you have in process() needs to be moved there. Also, add a counter that increments each time through the loop.
You defined values for HIGH and LOW (low should be 1, not 0), use them in the validation function rather than specific numbers 1 and 100. I like that you passed the target as a parameter to the function, rather than accessing the defined value. Perhaps your validation function should also be passed the high and low limits as parameters, making it more easily reused.
The reason your code does not catch illegal guesses is that you have the test for range as part of the if..else if chain, that first tells you too high/low. Thus, a guess of 105 is first found as too high, and the range test never gets activated. This will be fixed by breaking the range checking and guess testing into two functions. Within the function you currently have, if you tested for too high/low first, then testing for comparison to the target, you'd get correct outcome.
And by the way, this sounds like a real evil book if it's got you doing programs with functions and loops in chap 2. Most all the texts I have need 5 or 6 chapters to get that far along. May I ask the title/author of the book?
Val
In any case, the method of solving your problem will be essentially the same.
Reread the instructions. Your function is doing too much. First, it's doing two jobs (validating guess and testing if guess is correct.) Secondly, it's doing more than is asked, in that you have the function giving the output to the user. Each function should be very much like the mathematical idea of a function - some value(s) go in, a value comes out. In both cases, the functions should be returning an int based on the relationship of guess to the valid range, or guess relative to the secret number. Both these functions need only be a couple lines long.
You're on the right track with your loop in main( ), but the output you have in process() needs to be moved there. Also, add a counter that increments each time through the loop.
You defined values for HIGH and LOW (low should be 1, not 0), use them in the validation function rather than specific numbers 1 and 100. I like that you passed the target as a parameter to the function, rather than accessing the defined value. Perhaps your validation function should also be passed the high and low limits as parameters, making it more easily reused.
The reason your code does not catch illegal guesses is that you have the test for range as part of the if..else if chain, that first tells you too high/low. Thus, a guess of 105 is first found as too high, and the range test never gets activated. This will be fixed by breaking the range checking and guess testing into two functions. Within the function you currently have, if you tested for too high/low first, then testing for comparison to the target, you'd get correct outcome.
And by the way, this sounds like a real evil book if it's got you doing programs with functions and loops in chap 2. Most all the texts I have need 5 or 6 chapters to get that far along. May I ask the title/author of the book?
Val
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
![]() |
Similar Threads
- Number Guessing game problem (C++)
- 2D guessing game (Python)
- HELP creating a multiserver game in VB winsock (Visual Basic 4 / 5 / 6)
Other Threads in the C++ Forum
- Previous Thread: Game Programming for Slot machines
- Next Thread: [Question]who to make a computer choose a random number??
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






