Number guessing game problem

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2007
Posts: 3
Reputation: onlyled12 is an unknown quantity at this point 
Solved Threads: 0
onlyled12 onlyled12 is offline Offline
Newbie Poster

Number guessing game problem

 
0
  #1
Oct 9th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Number guessing game problem

 
0
  #2
Oct 9th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 3
Reputation: onlyled12 is an unknown quantity at this point 
Solved Threads: 0
onlyled12 onlyled12 is offline Offline
Newbie Poster

Re: Number guessing game problem

 
0
  #3
Oct 9th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 3
Reputation: onlyled12 is an unknown quantity at this point 
Solved Threads: 0
onlyled12 onlyled12 is offline Offline
Newbie Poster

Re: Number guessing game problem

 
0
  #4
Oct 9th, 2007
Here is what I have so far.
  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
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 3
Reputation: auxny is an unknown quantity at this point 
Solved Threads: 0
auxny auxny is offline Offline
Newbie Poster

Re: Number guessing game problem

 
-2
  #5
Oct 9th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Number guessing game problem

 
2
  #6
Oct 9th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 2,462
Reputation: zandiago is on a distinguished road 
Solved Threads: 25
Featured Poster
zandiago's Avatar
zandiago zandiago is offline Offline
Nearly a Posting Maven

Re: Number guessing game problem

 
0
  #7
Oct 12th, 2007
i did this program with the loops and not needing a separate functions from main.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Number guessing game problem

 
0
  #8
Oct 12th, 2007
Originally Posted by zandiago View Post
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.
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 2,462
Reputation: zandiago is on a distinguished road 
Solved Threads: 25
Featured Poster
zandiago's Avatar
zandiago zandiago is offline Offline
Nearly a Posting Maven

Re: Number guessing game problem

 
0
  #9
Oct 12th, 2007
Oh goodness, old age setting in one me...i read through the assignment question 2 qucikly...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC