446,408 Members | 945 Online
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
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 #include  using namespace std;   int randomNumber(int x, int y); int maxGuesses(int x); int main() {    int minNum, maxNum, maxGuesses;    char playAgain;    do    {       cout << "Enter a minimum number: ";       cin >> minNum;       cout << "\nEnter a maximum number: ";       cin >> maxNum;       maxGuesses();       randomNumber(minNum, maxNum);       compareNum(realNumber, guess);       cout << "Play again?"       cin >> playAgain;    while (playAgain != "N")      cout << "Bye!";      return 0; }   compareNum(int realNumber, int guess) {    if (realNumber > guess)       cout << "too high!"    else if       cout << "too low!"    else       cout << "You win!";    return 0; }   maxGuesses() {    int guesses = 0;    cout << "Enter maximum guesses: "    cin >> guesses;    return guesses; }   randomNumber(minNum, maxNum); {    int realNumber = rand() % maxNum + minNum;    return realNumber; } Feb 25 '07 #3

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

 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   (rand() %(maxNum- minNum+1)) + minNum;   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