•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,563 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,508 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 1417 | Replies: 7
![]() |
•
•
Join Date: Dec 2006
Posts: 7
Reputation:
Rep Power: 0
Solved Threads: 0
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.
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;
}•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,541
Reputation:
Rep Power: 40
Solved Threads: 972
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:
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.
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.
tuxation.com - Linux articles, tutorials, and discussions
•
•
Join Date: Dec 2006
Posts: 7
Reputation:
Rep Power: 0
Solved Threads: 0
•
•
•
•
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:
Hmm... try what I suggested with values of -1, 0 and 1.
For example:
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.
For example:
enum {lower_guess=-1, no_guess, higher_guess};
int prev_guess; // use this for keeping track of previous guessThen 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.
Last edited by John A : Jan 29th, 2007 at 10:58 pm.
tuxation.com - Linux articles, tutorials, and discussions
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,541
Reputation:
Rep Power: 40
Solved Threads: 972
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.
Nevermind my bloated-and-overly-complicated code solution.
tuxation.com - Linux articles, tutorials, and discussions
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- 2D guessing game (Python)
- How to make a C++ game?? (C++)
- HELP creating a multiserver game in VB winsock (Visual Basic 4 / 5 / 6)
- Random guessing game (C++)
- Help w/ constructors (Java)
- Anybody can help me make a game? (Java)
- How do i create a simple game using c++?? (C++)
- Applet or Application? (Java)
Other Threads in the C++ Forum
- Previous Thread: please i need help in my C++ assignment
- Next Thread: C++ Builder.



Linear Mode