By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,408 Members | 945 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,408 IT Pros & Developers. It's quick & easy.

number guessing game

P: 2
I have to do the following:

For this project, you are to create a simple number guessing game. The program should
begin by asking the user for a range (i.e. a minimum number and a maximum number) in
which the random number should fall. Next, the program should ask for a maximum
number of guesses to allow. The program should then allow the player to attempt to
guess the number giving the maximum allowable guesses. The program should give the
player hints along the way, telling them if their current guess is too big or too small.

Write a function that asks the player for the maximum number of guesses.
Write a function that asks the player for the range (the minimum and maximum
values).
Write a function that takes the player's current guess and the actual number and
tells the player if they are too high or too low.
Write a function that takes the minimum and maximum values from the user and
returns a random number in that range.

My main problem is, I'm not sure how to do the range function... since you can only return one value I don't know how to take two numbers and return them to use in the randomNum function. Please help
Feb 25 '07 #1
Share this Question
Share on Google+
4 Replies


100+
P: 1,646
If I have to return more than one value from a function I can either send references to my variables as parameters or I can change the values of global variables. This is often just a matter of style.
Feb 25 '07 #2

P: 2
Unfortunately I don't have a compiler at home so I can't test this, but this is what I got so far.


Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int randomNumber(int x, int y);
  5. int maxGuesses(int x);
  6. int main()
  7. {
  8.    int minNum, maxNum, maxGuesses;
  9.    char playAgain;
  10.    do
  11.    {
  12.       cout << "Enter a minimum number: ";
  13.       cin >> minNum;
  14.       cout << "\nEnter a maximum number: ";
  15.       cin >> maxNum;
  16.       maxGuesses();
  17.       randomNumber(minNum, maxNum);
  18.       compareNum(realNumber, guess);
  19.       cout << "Play again?"
  20.       cin >> playAgain;
  21.    while (playAgain != "N")
  22.  
  23.    cout << "Bye!";
  24.  
  25.    return 0;
  26. }
  27.  
  28. compareNum(int realNumber, int guess)
  29. {
  30.    if (realNumber > guess)
  31.       cout << "too high!"
  32.    else if
  33.       cout << "too low!"
  34.    else
  35.       cout << "You win!";
  36.    return 0;
  37. }
  38.  
  39. maxGuesses()
  40. {
  41.    int guesses = 0;
  42.    cout << "Enter maximum guesses: "
  43.    cin >> guesses;
  44.    return guesses;
  45. }
  46.  
  47. randomNumber(minNum, maxNum);
  48. {
  49.    int realNumber = rand() % maxNum + minNum;
  50.    return realNumber;
  51. }
Feb 25 '07 #3

100+
P: 1,646
Thank you for sharing :)
Feb 25 '07 #4

DeMan
100+
P: 1,806
At a quick glance, I think your randomNumber function may not be doing quite what you want.

I'm not quite sure on operator precedence but either (i think the first)
if I put in minimum number as 10 and maximyum as 50, then your method could
1) give me number between 10 and 59
OR
2)give me an answer as high as 59 (I think), and as low as 0.

I think what you want to do is
Expand|Select|Wrap|Line Numbers
  1.  
  2. (rand() %(maxNum- minNum+1)) + minNum;
  3.  
This would make the span mof numbers the difference between min and max (inclusive-> hence the +1) and the lowest number min.
Feb 25 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.