Thread: Function Help

  1. #1
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719

    Function Help

    Code:
    #include <iostream>
    
    using namespace std;
    
    
    int mMenu();
    int playgame();
    int about();
    
    int main()
    {
       
    
    int mMenu();
    {    
        int selection;
        system("CLS");
        cout << "1. Play Game" << endl;
        cout << "2. About Game" << endl;
        cout << "3. Exit" << endl;
        cin >> selection;
        switch (selection){
               case 1:
                    system("CLS");
                    system("PAUSE");
                    int playgame();
                    break;
               case 2:
                    system("CLS");
                    system("PAUSE");
                    int about();
                    break;
               case 3:
                    return 0;
                    break;
               default:
                       system("CLS");
                       cout << "INCORRECT" << endl;
                       system("PAUSE");
                       return 0;
                       break;
                       }
                       int about();  
                       {
                           system("CLS");
                           cout << "This is what the game is about" << endl;
                           }
                       int playgame();
                       {
                  system("CLS");  
                 cout << "This is the game" << endl;
                           }
                           cin.get();
                           cin.ignore();
                           }
                           }

    When I go to run it, and go to about game, it comes up with the cout from playgame();. I dont know how to stop it from doing that.
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  2. #2
    Registered User
    Join Date
    Dec 2004
    Posts
    32
    You function stuff is wrong. Move it outside of main and make them void.
    Code:
    #include <iostream>
    
    using namespace std;
    
    
    void mMenu();
    void playgame();
    void about();
    
    int main()
    {
    	mMenu();
    	cin.get();
    	cin.ignore();
    	return 0;	
    }
    void mMenu()
    {    
    	int selection;
    	//system("CLS");
    	cout << "1. Play Game" << endl;
    	cout << "2. About Game" << endl;
    	cout << "3. Exit" << endl;
    	cin >> selection;
    	switch (selection)
    	{
    	case 1:
    		//system("CLS");
    		//system("PAUSE");
    		playgame();
    		break;
    	case 2:
    		//system("CLS");
    		//system("PAUSE");
    		about();
    		break;
    	case 3:
    		return;
    		break;
    	default:
    		//system("CLS");
    		cout << "INCORRECT" << endl;
    		//system("PAUSE");
    		return;
    		break;
    	}
    }
    void playgame()
    {
    	//system("CLS");  
    	cout << "This is the game" << endl;
    }
    void about()
    {
    	//system("CLS");
    	cout << "This is what the game is about" << endl;
    }

  3. #3
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    It works now. But I don't understand void. Could someone explain why my code didn't work before.Thanks!!!
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    32
    Quote Originally Posted by Sentral
    It works now. But I don't understand void. Could someone explain why my code didn't work before.Thanks!!!
    void did not make the difference in it working or not working. The problem was you had your functions in main you can not define a function inside a function.

    Function header,
    Example :
    return_type name_of_function(parameter list)

    Code:
    int playgame();
    This has the return type of int
    Code:
    int playgame()
    {
    	//system("CLS");  
    	cout << "This is the game" << endl;
    return 0;
    }
    That returns zero.
    You would want to return something if you are going to assign it to something.
    Code:
    int var1 = playgame();
    Code:
    void playgame();
    This has the return type of void
    Code:
    void playgame()
    {
    	//system("CLS");  
    	cout << "This is the game" << endl;
    }
    Take a look at this link,
    http://www.fredosaurus.com/notes-cpp/

    Look for "Control Flow" and to the right you will see "Functions" it will explain some more.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM