| | |
Number Guessing Game
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2009
Posts: 31
Reputation:
Solved Threads: 0
I am new to c++ and I'm kinda struggling. I'm trying to write a program to guess a number that the user inputs. The program should guess a number then the user should be prompted if it is too high or too low. then the program should guess again, intill it gets it right. Then display "I guessed your number of ( lets say 37) in (lets say 5) tries" it should always guess the users number in 7 tries or less. So first the user is prompted to pick a number between 1 and 100, then the computer should guess the number, the first guess should always be 50, the user is then asked if this is too high or too low or correct, then if high the computer should guess 75, then the user should be prompted again is the number high low or correct, so basically every time the computer guesses it should be between the last guess and the maxguess. once the user has indicated that the guess is correct the program should display "I guessed your number of " " in " " guesses!
Here is some pseudo code for the program
numberofTries 1
guess 50
highLowResponse
usersNumber
minGuess 0
maxGuess 100
prompt user for mumber
retrieve userNumber
check guess
if no match
numberofTries increment
ask if high or low
get highLowResponse
if high
maxGuess gets set to current guess
new guess is guess/2 + minGuess
if low
minGuess gets set to current guess
new guess is maxguess - guess/2
output guess and number of tries
So what i need is some help with the program itself and I would like to thank all of you in advance for your help.
Here is some pseudo code for the program
numberofTries 1
guess 50
highLowResponse
usersNumber
minGuess 0
maxGuess 100
prompt user for mumber
retrieve userNumber
check guess
if no match
numberofTries increment
ask if high or low
get highLowResponse
if high
maxGuess gets set to current guess
new guess is guess/2 + minGuess
if low
minGuess gets set to current guess
new guess is maxguess - guess/2
output guess and number of tries
So what i need is some help with the program itself and I would like to thank all of you in advance for your help.
Here is something that will start you off :
C++ Syntax (Toggle Plain Text)
#include<iostream> using namespace std; int main() { //Create a variable for : // Min number the user can input //Max number the user can input //The number of guesses //The winning number //use random number generator to assign the winning number a value //From range [min,max] //Create a do while loop //Ask user for input //Check If its '>' , '<' , ' ==' the winning number and print certain statements accordingly return 0; }
1) Prove that the area of a circle is pi*r^2, where "r" is the radius of the circle. 2) Problem 2[b]solved by : jonsca
Use a do while loop
C++ Syntax (Toggle Plain Text)
int winningNumber = 42; int guessNumber = -1; do { //ask user to guess a number //check if its equal or not. Then determine the steps from there }while(winningNumber != guessNumber)
1) Prove that the area of a circle is pi*r^2, where "r" is the radius of the circle. 2) Problem 2[b]solved by : jonsca
•
•
Join Date: Sep 2009
Posts: 31
Reputation:
Solved Threads: 0
okay, here is what i have so far,
The problem i'm having now is the if you enter a number like lets say 77 the computer guesses 50 first like it should then the user enters l for low then the computer guesses 75 once again like it should, again the user enters l for low then the computer guesses 88, then the user enters h for high and the computers next guess is 44 but it shouldn't guess anything lower than 75 because it has already been determined that the number is higher than 75. so how can i fix this. additionally i need a way to tell the computer that the guess is correct once this happens.
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int _main { int theNumber; int computerGuess; int numGuesses = 1; char answer; int max; int min; cout << "Enter a number between 1 and 100:" << endl; cout << "Press h if my guess is high or l if my guess is low." << endl; cin >> theNumber; computerGuess = 50; while (numGuesses <= 5) { cout << "I guess " << computerGuess << endl; cin >> answer; if (answer == 'l') { max = 100; min = computerGuess + 1; computerGuess = (max + min)/2; numGuesses ++; } else if (answer == 'h') { max = computerGuess; min = 0; computerGuess = (max + 1)/2; numGuesses ++; } } return 0; }
The problem i'm having now is the if you enter a number like lets say 77 the computer guesses 50 first like it should then the user enters l for low then the computer guesses 75 once again like it should, again the user enters l for low then the computer guesses 88, then the user enters h for high and the computers next guess is 44 but it shouldn't guess anything lower than 75 because it has already been determined that the number is higher than 75. so how can i fix this. additionally i need a way to tell the computer that the guess is correct once this happens.
OH, I think you don't need have the user input a number. You need
to just have the computer guess a number, and the user should say
if its high,low, or correct.
In that case, you should have a function called average(int,int), that
takes 2 ints and returns its average. The two ints passed will
be your min and max.
Now see if the average is correct. If its too low
then make you min = average, and else if its too high make your
max = avg, else its correct. From there try to figure out the rest.
to just have the computer guess a number, and the user should say
if its high,low, or correct.
In that case, you should have a function called average(int,int), that
takes 2 ints and returns its average. The two ints passed will
be your min and max.
C++ Syntax (Toggle Plain Text)
min = 0; max = 100; int avg = average(min,max); //50
then make you min = average, and else if its too high make your
max = avg, else its correct. From there try to figure out the rest.
1) Prove that the area of a circle is pi*r^2, where "r" is the radius of the circle. 2) Problem 2[b]solved by : jonsca
•
•
Join Date: Sep 2009
Posts: 31
Reputation:
Solved Threads: 0
well the only problem with that is we haven't covered functions yet, so I cant use one in this code. I know the problem is between lines 26 and 39 and im pretty sure its in the computer guess line.
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { int theNumber; int computerGuess = 50; int numGuesses = 1; char answer; int max = 100; int min = 0; cout << "Enter a number between 1 and 100:" << endl; cin >> theNumber; computerGuess = 50; while (numGuesses <= 7) { cout << "I guess " << computerGuess << endl; cout << "Press h if guess is high,l ifguess is low, or c if correct." << endl; cin >> answer; if (answer == 'h') { max = computerGuess - 1; min = 0; computerGuess = ((max - min)/2 ) + min; numGuesses ++; } else if (answer == 'l') { max = 100; min = computerGuess + 1; computerGuess = (max + min)/2; numGuesses ++; } else if (answer == 'c') { cout << "I guessed your number of " << theNumber << " in " << numGuesses << " guesses!" << endl; } } return 0; }
You need to change the limits of min or max depending whether
the guess number is high or low.
There is 1 bug in this code. I'll let you figure that out
the guess number is high or low.
There is 1 bug in this code. I'll let you figure that out
C++ Syntax (Toggle Plain Text)
do { cout<<"Is your number : "<< mid << " ? \n\n"; cout<<"Too (h)igh\n"; cout<<"Too (l)ow\n"; cout<<"(c) orrect\n"; cout<<"(input) : "; cin >> pick; switch(pick) { //if its too high then now max should be mid and the guess number should be from 0 to mid or min to max case 'h' : max = mid; mid = (int)ceil((min + max)/2.0); break; case 'l' : min = mid; mid = (int)ceil((min + max)/2.0); break; case 'c' : gameOver = true; break; } cout<<"\n\n"; }while(!gameOver);
Last edited by firstPerson; Sep 26th, 2009 at 2:28 pm.
1) Prove that the area of a circle is pi*r^2, where "r" is the radius of the circle. 2) Problem 2[b]solved by : jonsca
![]() |
Similar Threads
- GUI Number Guessing Game Problem (Java)
- Number Guesssing Game Help (C++)
- stuck with guessing game please help (C)
- Guessing Game In C Help - High Score (C)
- guessing game program problem (C++)
- Number guessing game problem (C++)
- Number Guessing game problem (C++)
Other Threads in the C++ Forum
- Previous Thread: Windows Forms and remote thread
- Next Thread: link list n queue ...workin code just a small help needed
Views: 1533 | Replies: 7
| Thread Tools | Search this Thread |
Tag cloud for c++, game, guess, guessing, number, program, programing
account add api application array arrays assignment background based beginner binary bitmap blackjack borland c# c++ char char* class code community compile computer console conversion data design desktop development download encryption error evc faq file fstream function functions game games gdi+ graph hyper int intellectualproperty java line linux load math maximum microsoft newbie news nintendo number output partnership pause php playstation prime problem program programming project ps3 python read recursion recursive rpg search site software sony sql stream string strings struct studio subclass system template templates test text tree tutorial url user vbnet vector visual wii win32 word xbox xbox360







