Number Guessing Game

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2009
Posts: 31
Reputation: itzaaron is an unknown quantity at this point 
Solved Threads: 0
itzaaron itzaaron is offline Offline
Light Poster

Number Guessing Game

 
0
  #1
Sep 25th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,454
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 189
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso

Re: Number Guessing Game

 
0
  #2
Sep 25th, 2009
Here is something that will start you off :
  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. //Create a variable for :
  8. // Min number the user can input
  9. //Max number the user can input
  10. //The number of guesses
  11. //The winning number
  12.  
  13. //use random number generator to assign the winning number a value
  14. //From range [min,max]
  15.  
  16. //Create a do while loop
  17. //Ask user for input
  18. //Check If its '>' , '<' , ' ==' the winning number and print certain statements accordingly
  19.  
  20. return 0;
  21. }
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 31
Reputation: itzaaron is an unknown quantity at this point 
Solved Threads: 0
itzaaron itzaaron is offline Offline
Light Poster

Re: Number Guessing Game

 
0
  #3
Sep 25th, 2009
okay but what about loops, what kind should i use and i do i set them up? and a random number generator can not be used in this particular program, all numbers used should be predetermined in the code.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,454
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 189
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso

Re: Number Guessing Game

 
0
  #4
Sep 25th, 2009
Use a do while loop
  1. int winningNumber = 42;
  2. int guessNumber = -1;
  3. do
  4. {
  5. //ask user to guess a number
  6. //check if its equal or not. Then determine the steps from there
  7. }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
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 31
Reputation: itzaaron is an unknown quantity at this point 
Solved Threads: 0
itzaaron itzaaron is offline Offline
Light Poster

Re: Number Guessing Game

 
0
  #5
Sep 25th, 2009
okay, here is what i have so far,

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int _main
  6. {
  7. int theNumber;
  8. int computerGuess;
  9. int numGuesses = 1;
  10. char answer;
  11. int max;
  12. int min;
  13.  
  14. cout << "Enter a number between 1 and 100:" << endl;
  15. cout << "Press h if my guess is high or l if my guess is low." << endl;
  16. cin >> theNumber;
  17.  
  18. computerGuess = 50;
  19.  
  20. while (numGuesses <= 5)
  21.  
  22. {
  23. cout << "I guess " << computerGuess << endl;
  24. cin >> answer;
  25.  
  26. if (answer == 'l')
  27. {
  28. max = 100;
  29. min = computerGuess + 1;
  30. computerGuess = (max + min)/2;
  31.  
  32. numGuesses ++;
  33. }
  34.  
  35. else if (answer == 'h')
  36. {
  37. max = computerGuess;
  38. min = 0;
  39. computerGuess = (max + 1)/2;
  40.  
  41. numGuesses ++;
  42. }
  43. }
  44. return 0;
  45.  
  46. }

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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,454
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 189
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso

Re: Number Guessing Game

 
0
  #6
Sep 25th, 2009
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.
  1. min = 0;
  2. max = 100;
  3. int avg = average(min,max); //50
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.
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 31
Reputation: itzaaron is an unknown quantity at this point 
Solved Threads: 0
itzaaron itzaaron is offline Offline
Light Poster

Re: Number Guessing Game

 
0
  #7
Sep 26th, 2009
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.

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int _tmain(int argc, _TCHAR* argv[])
  6. {
  7. int theNumber;
  8. int computerGuess = 50;
  9. int numGuesses = 1;
  10. char answer;
  11. int max = 100;
  12. int min = 0;
  13.  
  14. cout << "Enter a number between 1 and 100:" << endl;
  15. cin >> theNumber;
  16.  
  17. computerGuess = 50;
  18.  
  19. while (numGuesses <= 7)
  20.  
  21. {
  22. cout << "I guess " << computerGuess << endl;
  23. cout << "Press h if guess is high,l ifguess is low, or c if correct." << endl;
  24. cin >> answer;
  25.  
  26. if (answer == 'h')
  27. {
  28. max = computerGuess - 1;
  29. min = 0;
  30. computerGuess = ((max - min)/2 ) + min;
  31. numGuesses ++;
  32. }
  33.  
  34. else if (answer == 'l')
  35. {
  36. max = 100;
  37. min = computerGuess + 1;
  38. computerGuess = (max + min)/2;
  39. numGuesses ++;
  40. }
  41. else if (answer == 'c')
  42. {
  43. cout << "I guessed your number of " << theNumber << " in " << numGuesses << " guesses!" << endl;
  44. }
  45. }
  46. return 0;
  47.  
  48. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,454
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 189
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso

Re: Number Guessing Game

 
0
  #8
Sep 26th, 2009
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
  1. do
  2. {
  3. cout<<"Is your number : "<< mid << " ? \n\n";
  4. cout<<"Too (h)igh\n";
  5. cout<<"Too (l)ow\n";
  6. cout<<"(c) orrect\n";
  7. cout<<"(input) : ";
  8. cin >> pick;
  9.  
  10. switch(pick)
  11. { //if its too high then now max should be mid and the guess number should be from 0 to mid or min to max
  12. case 'h' : max = mid;
  13. mid = (int)ceil((min + max)/2.0);
  14. break;
  15.  
  16. case 'l' : min = mid;
  17. mid = (int)ceil((min + max)/2.0);
  18. break;
  19.  
  20. case 'c' : gameOver = true; break;
  21. }
  22.  
  23. cout<<"\n\n";
  24.  
  25. }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
Reply With Quote Quick reply to this message  
Reply


Message:




Views: 1533 | Replies: 7
Thread Tools Search this Thread



Tag cloud for c++, game, guess, guessing, number, program, programing
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC