944,051 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4133
  • C++ RSS
Oct 9th, 2007
0

Number guessing game problem

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
onlyled12 is offline Offline
3 posts
since Oct 2007
Oct 9th, 2007
0

Re: Number guessing game problem

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
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Oct 9th, 2007
0

Re: Number guessing game problem

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
onlyled12 is offline Offline
3 posts
since Oct 2007
Oct 9th, 2007
0

Re: Number guessing game problem

Here is what I have so far.
C++ Syntax (Toggle Plain Text)
  1. #include <stdio.h>
  2.  
  3. #define TARGET 17
  4. #define LOW 0
  5. #define HIGH 100
  6. #define TRUE 1
  7. #define FALSE 0
  8.  
  9. int process( int guess, int target );
  10. int main( void )
  11.  
  12. {
  13. int a_guess, correct;
  14. correct = FALSE;
  15. printf( "Welcome to the game of Guess It!\n" );
  16. printf( "I will choose a number between %d and %d.\n", LOW, HIGH );
  17. printf( "You will try to guess that number. If you guess wrong,\n" );
  18. printf( "I will tell you if you guessed too high or too low.\n" );
  19. while ( correct == FALSE ) {
  20. printf( "Try to guess it now. " );
  21. scanf( "%d", &a_guess );
  22. correct = process( a_guess, TARGET );
  23. }
  24. return 0;
  25. }
  26.  
  27. int process( int guess, int target )
  28. {
  29. if ( guess < target )
  30. printf( "Too low!\n" );
  31. else if ( guess > target )
  32. printf( "Too high!\n" );
  33. else if ( guess > 100 )
  34. printf( "Illegal guess. Your guess must be between 1 and 100.\n" )
  35. else if (guess < 1 )
  36. printf( "Illegal guess. Your guess must be between 1 and 100.\n" )
  37. else {
  38. printf( "You guessed it!\n" );
  39. return TRUE;
  40. }
  41. return FALSE;
  42. }
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.
Last edited by Ancient Dragon; Oct 9th, 2007 at 8:20 am. Reason: add code tags
Reputation Points: 10
Solved Threads: 0
Newbie Poster
onlyled12 is offline Offline
3 posts
since Oct 2007
Oct 9th, 2007
-2

Re: Number guessing game problem

it's simple, you go to the modulator configurations in the visual basic nonsensical program, then you divide by -403.291 and you get the answer. It's simple and it's cool. Like Me.
Reputation Points: 7
Solved Threads: 0
Newbie Poster
auxny is offline Offline
3 posts
since Oct 2007
Oct 9th, 2007
2

Re: Number guessing game problem

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
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Oct 12th, 2007
0

Re: Number guessing game problem

i did this program with the loops and not needing a separate functions from main.
Featured Poster
Reputation Points: 129
Solved Threads: 26
Nearly a Posting Maven
zandiago is offline Offline
2,463 posts
since Jun 2007
Oct 12th, 2007
0

Re: Number guessing game problem

Click to Expand / Collapse  Quote originally posted by zandiago ...
i did this program with the loops and not needing a separate functions from main.
But the problem statement requires the two functions. I think that's the point of the problem.
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Oct 12th, 2007
0

Re: Number guessing game problem

Oh goodness, old age setting in one me...i read through the assignment question 2 qucikly...
Featured Poster
Reputation Points: 129
Solved Threads: 26
Nearly a Posting Maven
zandiago is offline Offline
2,463 posts
since Jun 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Game Programming for Slot machines
Next Thread in C++ Forum Timeline: [Question]who to make a computer choose a random number??





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC