Need help with functions

This is a discussion on Need help with functions within the C++ Programming forums, part of the General Programming Boards category; Hey guys I have just started c++ yesterday and I am now on Switch case and functions. I made this ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    2

    Need help with functions

    Hey guys I have just started c++ yesterday and I am now on Switch case and functions.

    I made this little program:

    Code:
    #include <iostream>
    using namespace std;
    
    void playgame();
    void loadgame();
    void playmultiplayer();
    void exit(); 
    
    int main()
    {
        int answer;
        
        cout << "1. Play A New Game\n"
             << "2. Load A Saved Game\n"
             << "3. Play Multiplayer\n"
             << "4. Exit\n";
        cin >> answer;
        
        switch (answer)
        {
               case 1:
                    playgame();
                    break;
               case 2:
                    loadgame();
                    break;
               case 3:
                    playmultiplayer();
                    break;
               case 4:
                    exit();
                    break;
               default:
                       cout << "Error Invalid Choice Closing!\n";
                       break;
    }
      cin.get();
    }
              void playgame()
              {
                  cout << "We Are Starting A New Game\n";
                   }
              void loadgame()
              {
                  cout << "Loading A Saved Game\n";
                   }
              void playmultiplayer()
              {
                  cout << "Searching For A Opened Server\n";
                   }
              void exit()
              {
                  cout << "What A Quiter! Come On Be A Man!\n";
                   }


    When I run it, it works perfectly fine it displays everything. But when I go to enter a option like say I hit a 1 to "play a game" the program exits really fast without printing anything to the screen. Just wanted to know how I could fix this thank you guys.

    I got this code from the tutorials "Switch case" but wanted to see if I could get it to work with functions to see how much I knew so far. OH Excellent Tutorials glad I found this site.


    --Mustang1968kid 17yr.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    This question was just asked in the thread below your's (First program). cin.get() should wait for you to press enter, you can also open a command prompt from the start menu and type the file name of your program.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    First, you should have a return statement in main(). Here is the program template all beginners should use:
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	
    	//your code here
    
    	return 0;
    }
    That would probably make it more obvious what is wrong with your program: your cin.get() is outside of main(), so it is never executed.
    Code:
    int main()
    {
        
    }
    cin.get();
    Your program is executed line by line starting at the top of main() and works sequentially downward. When you call a function, that causes execution to be transferred to the function. When the function finishes executing, execution is transferred back to main(). After all the lines of code in main() have been executed, the closing parenthesis in main() is encountered. When that happens, there is an implicit return statement:

    return 0;

    which causes your program to terminate execution. However, you should make that return statement explicit by including it in your program as the last line of main().
    Last edited by 7stud; 11-20-2005 at 12:54 PM.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    >> First, you should have a return statement in main().
    This isn't necessary. Some think it is good style, but it really doesn't matter either way.

    Also, in this case, the cin.get() is in the right place.

    The problem is like in the other thread, the call to get() gets the leftover newline from the previous call to cin >> answer. You need to ignore the leftover newline so that the cin.get() will wait for the user to hit enter again.

    To do this add a cin.ignore() or another cin.get() either after the call to cin >> answer or at the end of the main function where the current cin.get() is.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 02:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM

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