Hi there, I need to create a program that generates a random number, and asks the user to guess the number and the program tells them higher or lower, and if the program tells them higher, and they enter a number lower, or vice-versa, you call them a moron. For example,

Random number= 34
User enters- 17
Program says- higher
User enters- 10
Program says- moron higher

You must count the total number of guesses, and the number of time they were called moron.

I need help with the Moron counter, Thanks.

#include <iostream>
#include <time.h>
using namespace std;

int main ()
{
	int nGuess,nNum,nCount=0;
	srand((unsigned)time(0));
	nGuess=rand ()%50+1;
	while (nNum!=nGuess)
	{
		cout<<"Please enter a number between 1-50"<<endl;
		cin>>nNum;
		if (nNum==nGuess)
		{
			cout<<"Correct"<<endl;
			nCount+=1;
		}
		else if (nNum>nGuess)
		{
			cout<<"Lower"<<endl;
			nCount+=1;
		}
		else if (nNum<nGuess)
		{
			cout<<"Higher"<<endl;
			nCount+=1;
		}
	}
	cout<<"Total gueses equal "<<nCount<<endl;
	//cout<<"Total Moron's equal "<<MoronCount<<endl;
	return 0;
}

Recommended Answers

All 7 Replies

you need to add two more counters -- total number of morons and another counter to indicate whether the last guess was higher or lower.
For example:

else if (nNum>nGuess)
{
    if( last_guess < nGuess )
    {
          cout << "moron";
          moronCoujnt++;
    }
}

You need to keep track of whether or not you've already told the user whether he/she needs to go higher or lower - perhaps store it in an int; 0 means no instruction yet, 1 means higher, and -1 means lower.

Test this int value before you tell the user whether to go higher or lower, and then if it's correct reset the variable and print out the hint. If it's wrong, call them a moron and increment the moron counter.

you need to add two more counters -- total number of morons and another counter to indicate whether the last guess was higher or lower.
For example:

else if (nNum>nGuess)
{
    if( last_guess < nGuess )
    {
          cout << "moron";
          moronCoujnt++;
    }
}

i need help with this part

counter to indicate whether the last guess was higher or lower.
For example:

i need help with this part

Hmm... try what I suggested with values of -1, 0 and 1.

For example:

enum {lower_guess=-1, no_guess, higher_guess};
int prev_guess; // use this for keeping track of previous guess

Then if the guess needs to be higher, and prev_guess is 0, then set prev_guess to higher_guess, if the guess was lower, than set it to lower_guess, etc..

The trickier part is detecting morons :mrgreen:. If the value of your variable for keeping track of prev_guess is not 0, you need to check that the value is higher_guess if the guess needs to be higher, and lower if it's lower_guess. If it's not, call the user a moron and increment the moron counter.

Hope that made sense.

i need help with this part

I posted the answer to one half. Just copy it into the other half and make appropriate changes to the less-than-operator. I know you can do it if you think about it a few minutes. If you have been racking your brain several hours, then go watch TV or do something else for awhile.

I posted the answer to one half. Just copy it into the other half and make appropriate changes to the less-than-operator. I know you can do it if you think about it a few minutes. If you have been racking your brain several hours, then go watch TV or do something else for awhile.

Oh sorry, Ancient Dragon! My solution has way too much fluff; your method is much better.

Nevermind my bloated-and-overly-complicated code solution. ;)

Oh sorry, Ancient Dragon! My solution has way too much fluff; your method is much better.

Nevermind my bloated-and-overly-complicated code solution. ;)

You had a good solution too. Don't discount the power of multiple suggestions.:)

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.