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

This is a discussion on Beginner c++ programmer looking for suggestions.... within the C++ Programming forums, part of the General Programming Boards category; Hi All, thanks in advance for any help offered! I'm trying to write a program that plays the "High Low" ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    8

    Cool 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;
    }

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    please use code tags.

  3. #3
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    oops...

    [edit] 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.
    Last edited by ronin; 03-08-2003 at 12:30 PM.
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  4. #4
    Burning in Hell! Luigi's Avatar
    Join Date
    Nov 2002
    Posts
    117
    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

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    8
    Luigi, thanks so much! One question, what is the purpose of the return statement after calling the function endGame()?

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    the return statement will get you out of the fxn. if there is no need to continue with the fxn.

  7. #7
    Registered User
    Join Date
    Mar 2003
    Posts
    8
    THANK YOU!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-29-2009, 11:13 AM
  2. Beginner C programmer - My CSV reader
    By spadez in forum C Programming
    Replies: 6
    Last Post: 04-04-2009, 09:46 PM
  3. beginner c++ programmer compile errors
    By dodo10 in forum C++ Programming
    Replies: 4
    Last Post: 05-28-2008, 05:37 PM
  4. [C] - String Manipulation {Beginner Programmer}
    By INFERNO2K in forum C Programming
    Replies: 14
    Last Post: 05-21-2005, 12:34 PM
  5. Any beginner C++ programmer wants to.....
    By incognito in forum C++ Programming
    Replies: 5
    Last Post: 12-06-2001, 08:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21