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.

Recommended Answers

All 8 Replies

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

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.

Here is what I have so far.

#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;
}

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.

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.

commented: If you can't help at least be quiet. -1
commented: ??? +0

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

commented: *nods* +11
commented: To a fellow Dakotan, albeit the wrong one. +11

i did this program with the loops and not needing a separate functions from main.

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.

Oh goodness, old age setting in one me...i read through the assignment question 2 qucikly...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.