Thread: A beginner in trouble

1. A beginner in trouble

Hi all. I am a begginer, just started in school learning this. Well, I have a problem in one of my assingments:

Code:
```// a game where the computer has to guess a number that the
// user is thinking of.  I am a beginnner, and I haven't learned
// all the cool stuff yet - like using random number generator
// so I am trying to do this the simple way.  But this isn't working
// properly, something about the numbers and the
// guessing in the do-while loop.

#pragma argsused

#pragma argsused
#include <iostream.h>
#include <conio.h>
using namespace std;

int main()

{
int highNumb = 100;
int lowNumb = 1;
int guess;
char ans;

cout << "Think of a number between 1 and 100 \n";

cout << endl;
guess =(highNumb+lowNumb)/2;
cout << "is the number " << guess << " ?\n";
cout << endl;
cout << "is the number correct or is it too high or too low? (c,h,l)\n";
cin >> ans;

do
{
lowNumb = ++guess;
guess = (highNumb + lowNumb)/2;
cout << "the number is: "<< guess <<endl;
cin >> ans;
if (lowNumb >= highNumb)
cout << "CHEATER!! " <<endl;
}while (ans == 'H' || ans == 'h');

do
{
highNumb = --guess;
guess = (highNumb - lowNumb) ;
cout << "the number is: " << guess <<endl;
cin >> ans;
if (lowNumb >= highNumb)
cout << "CHEATER "<<endl;
}while (ans == 'L' || ans == 'l');

if (ans == 'R' || ans == 'r')
cout << "GENIOUS!!!"<<endl;

getch ();
return 0;
}```

2. Eldey,
As it's homework I won't rewrite this, but you're not asking whether the guess is correct, too high, or too low within the loop. You need something along these lines:

Code:
```...
...
do
{
cout << endl;
guess =(highNumb+lowNumb)/2;
cout << "is the number " << guess << " ?\n";
cout << endl;
cout << "is the number correct or is it too high or too low? (c,h,l)\n";
cin >> ans;

if (ans == 'H' || ans == 'h')
{
highNumb = guess;
}
if (and == 'L' || and == 'l')
{
lowNumb = guess;
}
} while (ans != 'L' && ans != 'l');
...
...```
and build in checks for cheating. This should get you started.

Edit your post and remove your e-mail address. Spammers scour boards such as these looking for e-mail addresses to send their junk to.

3. Ok thanks, but what am I doing wrong when the numbers don't go higher or lower, I am doing something wrong in the calculation?

4. Eldy--relook and Angoid's post line by line and ask yourself the reason for each line. The answer to this question: "what am I doing wrong when the numbers don't go higher or lower, I am doing something wrong in the calculation?" will then become clear.

Popular pages Recent additions