switch/case & functions

This is a discussion on switch/case & functions within the C++ Programming forums, part of the General Programming Boards category; When I run this (trying to use the tutorials) I get a 'parse error before if' for line 30 (see ...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    5

    switch/case & functions

    When I run this (trying to use the tutorials) I get a 'parse error before if' for line 30 (see notes in code). I can't figure out why!

    Code:
    #include <iostream.h>	
    #include <conio.h>	
    int playgame(int a);
    int loadgame(int b);
    int multiplayer(int c);
    int main()
    {  	 
      int input;	  
      cout<<"1. Play game"<<endl;
      cout<<"2. Load game"<<endl;
      cout<<"3. Play multiplayer"<<endl;
      cout<<"4. Exit"<<endl;
      cin>>input;	 
      switch (input)	
      {	   
       case 1: playgame(1);
    	   break;	 
       case 2:
    	  loadgame(2);
            break;	
       case 3:
            multiplayer(3);
    	  break;	
       case 4:
            return 0;   
      default: 	  
            cout<<"Error, bad input, quitting";	
      }
    }
      int playgame(if (a==1)   //parse error before 'if'
                      {
                      <<cout"Game loading...";
                      })
      int loadgame(if (b==2)
                      {
                      <<cout"Loading saved game...";
                      })
      int multiplayer(if (c==3)
                      {
                      <<cout"Multiplayer game loading...";
                      })
    {
      return 0;
    }

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    look at your curly braces. you close up the main function before the if.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    5

    cury brace

    I took those out and I'm still getting the same 'parse error before if'

    The reason I had closed off the main function was because the example for functions in tutorials had done that.

    Thanks for replying so quickly

  4. #4
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    int playgame( <-before the if. Obviously bad.

    wait, I'm looking down in that section of code and it is completely wrong. Are you trying to do functions or what?
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  5. #5
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    by doing this:
    Code:
    int playgame(int a);
    int loadgame(int b);
    int multiplayer(int c);
    You have set three functions, each taking a single int variable. Since the functions are of type int, they should return an integer as well.

    by doing the following:
    Code:
    playgame(1);
    you are making a euqal to 1 and starting the function. But Your function is not defined anywhere...the whole thing sort of beats the purpose...I would have something like this:

    Code:
    case 1:
        cout << "Game Loading...";
        playgame(); //and this function will start the game and so on
    You are making it more difficult then it really is,

    axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  6. #6
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297

    Re: switch/case & functions

    Code:
    #include <iostream.h>	
    #include <conio.h>	
    void playgame(int a);
    void loadgame(int b);
    void multiplayer(int c);
    int main()
    {  	 
      int input;	  
      cout<<"1. Play game"<<endl;
      cout<<"2. Load game"<<endl;
      cout<<"3. Play multiplayer"<<endl;
      cout<<"4. Exit"<<endl;
      cin>>input;	 
      switch (input)	
      {	   
       case 1:
         playgame(1);
    	   break;	 
       case 2:
    	  loadgame(2);
            break;	
       case 3:
            multiplayer(3);
    	  break;	
       case 4:
            return 0;   
      default: 	  
            cout<<"Error, bad input, quitting"<< endl;	
      }
    }
    void playgame(int a)
          {
          if (a==1)   //parse error before 'if'
                {
                cout << "Game loading..." endl;
                }
          }
    void loadgame(int b)
          {
          if (b==2)
                {
                 cout << "Loading saved game..." << endl;
                 }
          }
    void multiplayer(int c)
       {
       if (c==3)
              {
              cout <<"Multiplayer game loading..." << endl;
             }
       }
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
      int playgame(if (a==1)   //parse error before 'if'
                      {
                      <<cout"Game loading...";
                      })
      int loadgame(if (b==2)
                      {
                      <<cout"Loading saved game...";
                      })
      int multiplayer(if (c==3)
                      {
                      <<cout"Multiplayer game loading...";
                      })
    Code:
      int playgame(int a)
      {
                      cout << "Game loading..." << endl;
      }
      int loadgame(int b)
      {
                      cout << "Loading saved game..." << endl;
      }
      int multiplayer(int c)
      {
                      cout<< "Multiplayer game loading..." << endl;
      }

  8. #8
    Registered User
    Join Date
    Aug 2003
    Posts
    5

    Thanks :)

    Thanks for all the help, I appreciate it

    Torsin

  9. #9
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    One more thought/comment for you, Torsin

    An if-statement is not an expression. It doesn't evaluate to anything, or return anything.

    This means that you can't write: x = if(A==B0);

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