Thread: I need help please!

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

    I need help please!

    I am writing a basic game program to use as a reference later on. BUT if I try to use teh same variable in both it says multiple definitions, and if I dont init the variable it wont either. Here is what I am trying to do...
    FIRST .CPP
    Code:
    #include "main.h"
    #include "colors.h"
    int a;
    void mainmenu();
    void charmenu();
    int EXIT();
    void options();
    void newgame();
    void loadgame();
    //
    int main()
    {
    	mainmenu();
    	return 1;
    }
    void mainmenu()
    {
    	text_red;
    	system("cls");
    	cout << "****************\n";
    	cout << "*(1) New Game  *\n";
    	cout << "*(2) Load Game *\n";
    	cout << "*(3) Options   *\n";
    	cout << "*(4) Exit	  *\n";
    	cout << "****************\n";
    	text_darkgrey;
    	cout << "Selection: ";
    	cin >> a;
    	cin.ignore();
    	switch ( a ) {
    		   case 1:
    				newgame();
    				break;
    		   case 2:
    				break;
    		   case 3:
    				break;
    		   case 4:
    				EXIT();
    				break;
    		   default:
    				cout <<" Sorry but Please Input 1-4 please";
    				cout <<" Press ENTER to continue";
    				cin.get();
    				mainmenu();   
    				break;
    }
    }
    //
    int EXIT()
    {
    	system("cls");
    	cout.flush();
    	return (EXIT_SUCCESS);
    }
    //END OF EXIT
    and just a test .cpp looks like this.
    Code:
    #include "main.h"
    int a;
    //the following are the diffrent programs within the mother program to make this game playable
    void mainmenu();
    void charmenu();
    int EXIT();
    void options();
    void newgame();
    void loadgame();
    //end of menu setup//notice ALL menus except main and Exit are void
    void newgame()
    {
    	 cout << "New Game Test Cout Function";
    	 cin.get();
    	 cin >>a;
    	 cin.ignore();
    	 cout <<"Test 2 you entered "<< a <<" as a number";
    	 cin.get();
    	 }
    now the errors are
    Code:
      multiple definition of `a' 
      first defined here  
      ld returned 1 exit status
    I have tried not difing it in the newgame .cpp but it wont and defing it i get errors as well so what is wrong.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    In one of your source files, a must be declared as extern.

    Code:
    extern int a;
    Also:
    Code:
    void mainmenu()
    {
        text_red;
        system("cls");
        cout << "****************\n";
        cout << "*(1) New Game  *\n";
        cout << "*(2) Load Game *\n";
        cout << "*(3) Options   *\n";
        cout << "*(4) Exit	  *\n";
        cout << "****************\n";
        text_darkgrey;
        cout << "Selection: ";
        cin >> a;
        cin.ignore();
        switch ( a ) {
            case 1:
                newgame();
                break;
            case 2:
                break;
            case 3:
                break;
            case 4:
                EXIT();
                break;
            default:
                cout <<" Sorry but Please Input 1-4 please";
                cout <<" Press ENTER to continue";
                cin.get();
                mainmenu();   
                break;
        }
    }
    That recursive call to the mainmenu function is something that would be better accomplished by rearranging the code a bit and using a loop.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    I dont know really an other way of rearranging my code (and what is wrong with the recursive call)??

    Also thanks alot for the help, I knew I was forgetting something really simple lol

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    colors.h ?
    i dont have that header file, anyone can upload or tell me a site where i can download these?

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    oh it is for the text_color function. here it is if you want it (You have to have <windows.h> included tho, as it is a windows function. here it is if you want it.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    k thanks,

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    windows.h doesnt come with vc++ as well
    upload that to?

    EDIT: nvm i found it in a copy of dev c++

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    windows.h doesnt come with a microsoft compiler? how messed up is that.

  9. #9
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    yep it doesnt ^.^

  10. #10
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    windows.h doesnt come with vc++ as well
    Either your installation of VC is corrupted, or your include statement is wrong. VC does come with all windows headers, including windows.h


    Code:
    int EXIT()
    {
    	system("cls");
    	cout.flush();
    	return (EXIT_SUCCESS);
    }
    Nice idea, but it won't work the way you want it to. If you have a return in main, it will exit the application. A return in a function will only exit the function, not the application.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  11. #11
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by nvoigt
    Code:
    int EXIT()
    {
    	system("cls");
    	cout.flush();
    	return (EXIT_SUCCESS);
    }
    Nice idea, but it won't work the way you want it to. If you have a return in main, it will exit the application. A return in a function will only exit the function, not the application.
    But, calling it like this would work though wouldn't it: return EXIT();
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  12. #12
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    It does work tho. It works the because it is the last function in the .cpp. so when it returns there is no other code for the program to execute so it will work.I have this code in all my programs just cuzz it is easier to adjust my code if my exit is not within the main program.

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The fact that it is the last function defined in the cpp file means nothing. It only matters if it is the last function called. In your case, it would call system("cls") and cout.flush() when the user chooses option 4 to exit. However, as nvoigt said, the return (EXIT_SUCCESS) does nothing. The code that calls EXIT() doesn't use it's return value, and so it is ignored. The mainmenu() method finishes and control returns to main(), and then main() calls return 1.

    If you check the exit code of your program, you will see that it is 1, not 0 (EXIT_SUCCESS is 0). So the screen is being cleared, but the return(EXIT_SUCCESS) is ignored.

  14. #14
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    OH, so it will exit the program with or without the return(EXIT_SUCCESS) right?

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Yes.

Popular pages Recent additions subscribe to a feed