# Beginner c++ programmer looking for suggestions....

• 03-08-2003
Sheshi
Beginner c++ programmer looking for suggestions....
Hi All, thanks in advance for any help offered!

I'm trying to write a program that plays the "High Low" game. A user is prompted to type any number between 1-100, the computer generates a random number, and then lets you guess the number. Between each guess, the computer tells you whether to guess higher or lower, until you get the right answer.

I am a beginner here... this is my first program using functions, so please be gentle... The following program hangs after the first prompt and I can't figure out why...

Any suggestions??
_________________

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

#include <cstdlib>

int prompt(int);
void endGame();

int main()
{
enum status {TooHigh, TooLow, WON};
status gameStatus;
int guessNum = 0,
result;

cout << "Please enter a number between 1 and 100 (type 'q' to end the game): ";
cin >> guessNum;

if (guessNum == 'q')
endGame();

else
result = prompt(guessNum);

switch (result) {
case 1:
cout << "Please enter a lower number: ";
cin >> guessNum;
gameStatus = TooHigh;
break;
case 2:
cout << "Please enter a higher number: ";
cin >> guessNum;
gameStatus = TooLow;
break;
case 3:
cout << "Congrats!" << endl;
gameStatus = WON;
break;
}

if (gameStatus == TooHigh || gameStatus == TooLow) {
prompt(guessNum);
}

return 0;
}

int prompt(int number)
{
unsigned int randNumber = 1 + rand() % 100;
int x = number;

while (x != randNumber) {

if (x < randNumber)
x = 1;

else
if (x > randNumber)
x = 2;

else
if (x == randNumber)
x = 3;
}
return x;
}

void endGame()
{
cout << "Thanks for playing..." << endl;
return;
}
• 03-08-2003
alpha
• 03-08-2003
ronin
oops...

 you come back to main from endgame() That's a logic problem because the function will return to process the switch statement. Use exit in endgame if you wish to quit. [/edit]

I'll leave it there from now.
• 03-08-2003
Luigi
wow that was messy
I changed your program a lot but now it works and it way more effective and clearer.
there was too much functions and var for nothing there..
void endgame could be removed to but i tought u might want to keep it anyway if it is an homework..
If was an homework hope switch wasnt mandatory cause I removed it.
here goes:

Code:

```#include <iostream> using namespace std;   void prompt(); void endGame();   int main() {       prompt();       return 0; }     void prompt() {       enum status {TooHigh, TooLow, WON};       status gameStatus;       int guessNum = 0;       int randNumber = 1 + rand() % 100;       while(gameStatus != WON)       {           cout << "Please enter a number between 1 and 100 (type '0' to end the game): " << endl;           cin >> guessNum;           if (guessNum == 0)      //you cannot put char in int only numbers           {                 endGame();                 return;           }                          if (guessNum < randNumber)           {                 cout << "Too low!" << endl;                 gameStatus = TooHigh;           }           else if (guessNum > randNumber)           {                 cout << "Too high!"<< endl;                 gameStatus = TooLow;           }           else if (guessNum == randNumber)           {                 cout << "Congrats!" << endl;                 gameStatus = WON;           }       } }   void endGame() {       cout << "Thanks for playing..." << endl; }```
Luigi
• 03-08-2003
Sheshi
Luigi, thanks so much! One question, what is the purpose of the return statement after calling the function endGame()?
• 03-08-2003
alpha
the return statement will get you out of the fxn. if there is no need to continue with the fxn.
• 03-08-2003
Sheshi
THANK YOU!