Age guessing game
 CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

1. Junior Member
Join Date
Feb 2013
Posts
2

## Age guessing game

Write a program that predicts users’ age (0-128 years old) with at most 7 questions. The game starts with asking the user whether he/she is younger or older than G (an initial guess). The user responds with 'O’ for older', ‘Y for 'younger' and ‘X’ for ‘you got it!’
Use a while loop to ask user whether his/her age is ‘>’, ’<’ or ‘=’ the guess. Update the guess using the binary search method. Note that you are not allowed to use “break”. cant figure out where i'm going wrong. tried to change a few things when it wouldn't compile till i got lost in my own code.
Code:
```#include <iostream>
#include <cmath>

using namespace std;

int binary_search(int A[], int key, int imin, int imax)
{
char A = Y || y || O || o || X || x;

cout << "Are you 128 years old? Type O for older"                                    \

<< " Y for younger or X for 'you got it'" << endl;
cin >> Y || y || O || o || X || x;

if (A = Y || y)
}
/*****
int binary_search (int A[], int key, int imin = 0, int imax = 128)
{
while (imin < imax)
{
int imid = midpoint (i min, i max );
assert (i mid < imax);
if (A[imid] < key)
imin = imid + 1;
else
imax = imid;
}
if ((imax == imin) && (A[imin] == key))
return imin;
else
return KEY_NOT_FOUND;
}```

2. ## Re: Age guessing game

What could these expressions mean:
Originally Posted by DawnC
Code:
```    char A = Y || y || O || o || X || x;

....
if (A = Y || y)```

3. Junior Member
Join Date
Feb 2013
Posts
2

## Re: Age guessing game

The character A could be entered in as Y caps or y or O or o or X or x
where O represents being older than the originak guess, y is younger and x means the programmes guess is correct.

4. Elite Member Power Poster
Join Date
Apr 1999
Posts
27,449

## Re: Age guessing game

Originally Posted by DawnC
The character A could be entered in as Y caps or y or O or o or X or x
where O represents being older than the originak guess, y is younger and x means the programmes guess is correct.
1) C++ isn't the same as English-speak.

2) To compare you use ==, not=.

When specifying that a character can be equal to a set of characters, you have to use the correct boolean expression. How we say stuff in English doesn't equate to expressing the same thing in C++:
Code:
`if ( A == 'Y' || A == 'y' || A == 'o' || A == 'O')`
That is how you specify if A is equal to a set of values in C++.

Last, have you ever seen an expression as you wrote in your code in any C++ book or tutorial? If not, then there is a good reason you've never seen it. It's wrong.

Regards,

Paul McKenzie

5. Elite Member Power Poster
Join Date
Nov 2003
Location
Florida
Posts
12,518

## Re: Age guessing game

Originally Posted by DawnC
The character A could be entered in as Y caps or y or O or o or X or x
where O represents being older than the originak guess, y is younger and x means the programmes guess is correct.
There seems to be a trend lately where people just try to guess at the syntax. What you posted isn't even close to correct, and honestly, pointing out the mistakes seems kind of pointless. Take your time with a good book and learn the basics. You won't get anywhere throwing stuff at the wall to see what sticks.

6. ## Re: Age guessing game

"There seems to be a trend lately where people just try to guess at the syntax"
It's not just on codeguru forums either. Other forums are also having these types of questions posted.

There's also seems to be a trend where people don't, or don't know how, to debug programs properly that compile but don't perform as expected.

I'm going to be kind to the students and say the problem is that some tutors are setting some c++ programming problems without giving the students enough understanding in program design, testing/debugging and language syntax for some students to competently complete the designated assignment. Learning to program in a language like c++ from scratch can be terribly difficult at first until it all starts to fall into place. That's where having a good introductory book on c++ programming really helps. It takes practice and experience to 'think like a progammer'.

7. Elite Member Power Poster
Join Date
Apr 1999
Posts
27,449

## Re: Age guessing game

Originally Posted by GCDEF
There seems to be a trend lately where people just try to guess at the syntax.
And to add, there is no computer language that, as far as I know, can be learned from guesswork, regardless of how simple or easy the language may seem to be.

Even a professional programmer using a tough language like C++ would need a tutorial, book, examples, on getting a BASIC program to work properly, if that person never saw BASIC before. I wish the tutors would emphasize that point about guessing -- learning programming is not a guessing game, but as you stated, it seems to be a trend now.

Regards,

Paul McKenzie

8. Elite Member Power Poster
Join Date
Nov 2003
Location
Florida
Posts
12,518

## Re: Age guessing game

Originally Posted by Paul McKenzie
And to add, there is no computer language that, as far as I know, can be learned from guesswork, regardless of how simple or easy the language may seem to be.

Even a professional programmer using a tough language like C++ would need a tutorial, book, examples, on getting a BASIC program to work properly, if that person never saw BASIC before. I wish the tutors would emphasize that point about guessing -- learning programming is not a guessing game, but as you stated, it seems to be a trend now.

Regards,

Paul McKenzie
I was a COBOL programmer for years before switching to C++. Even then it took about three months before I was moderately productive at C++ and at least a year before I was comfortable with it.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•