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
vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
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
vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
i did this program with the loops and not needing a separate functions from main.
zandiago
Nearly a Posting Maven
2,480 posts since Jun 2007
Reputation Points: 129
Solved Threads: 26
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.
vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
Oh goodness, old age setting in one me...i read through the assignment question 2 qucikly...
zandiago
Nearly a Posting Maven
2,480 posts since Jun 2007
Reputation Points: 129
Solved Threads: 26