Thread: Defining Functions.

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    18

    Defining Functions.

    I was working on Lesson 5: Switching Cases of cprogramming.com's C++ tutorials however the code given was incomplete, but the author said with little work I could define the functions that were left undefined in the source and get a running program...So I gave it a try, and bam - it didn't work! I looked over the code I studied from Chapter 4 on Functions and really can't see where I am messing up, so if someone could give me a hand, I'd appreciate it. Here is my code:
    Code:
    #include <iostream>
    using namespace std;
    #include <conio.h>
    int playgame();
    int loadgame();
    int playmultiplayer()
    int main()
    {
        int input;
        cout<<"1. Play game";
        cout<<"2. Load game";
        cout<<"3. Play multiplayer";
        cout<<"4. Exit";
        cin>>input;
        switch (input)
        {
            case 1: playgame();
                    break;
            case 2: loadgame();
                    break;
            case 3: playmultiplayer();
                    break;
            case 4: 
                    return 0;
            default:
                    cout<<"Error, bad input, quitting";
        }
    int playgame()
    {
        cout<<"You are playing the game!"
    }
    int loadgame()
    {
        cout<<"The game is loading!"
    }
    int playmultiplayer()
    {
        cout<<"You are playing multiplayer!"
    }    
        return 0;
    }
    C:/Documents and Settings/David Mackey/My Documents/cprograms/switchcases.cpp: In
    function `int main()':
    C:/Documents and Settings/David Mackey/My Documents/cprograms/switchcases.cpp:29: parse
    error before `{' token
    C:/Documents and Settings/David Mackey/My Documents/cprograms/switchcases.cpp: In
    function `int loadgame()':
    C:/Documents and Settings/David Mackey/My Documents/cprograms/switchcases.cpp:35: parse
    error before `}' token
    C:/Documents and Settings/David Mackey/My Documents/cprograms/switchcases.cpp: In
    function `int playmultiplayer()':
    C:/Documents and Settings/David Mackey/My Documents/cprograms/switchcases.cpp:39: parse
    error before `}' token
    Execution terminated

    Thanks for your help in advance.
    Respectfully,
    David.
    - http://www.civilwarsearch.com/ - Civil War Search Directory.
    - http://www.dhq.nu/hutsell/ - Four Free Computer Wargames.
    - http://www.debaunart.com/ - Original and Print Watercolor Artwork.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You have this problem
    http://cboard.cprogramming.com/showt...threadid=48337

    Functions inside functions
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Code:
    int playmultiplayer()
    Missing a semicolon.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Additionally, if your functions don't return anything, give them a 'void' return type.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  5. #5
    Registered User
    Join Date
    Dec 2003
    Posts
    18
    How do I make a function return a void type? I tried making sure that my functions where outside the main, and correcting all the errors you pointed out, but I'm still having problems. Here is the new code:
    Code:
    #include <iostream>
    using namespace std;
    #include <conio.h>
    int playgame();
    int loadgame();
    int playmultiplayer();
    int main()
    {
        int input;
        cout<<"1. Play game";
        cout<<"2. Load game";
        cout<<"3. Play multiplayer";
        cout<<"4. Exit";
        cin>>input;
        switch (input)
        {
            case 1: playgame();
                    break;
            case 2: loadgame();
                    break;
            case 3: playmultiplayer();
                    break;
            case 4: 
                    return 0;
            default:
                    cout<<"Error, bad input, quitting";
        }
    }
    int playgame()
    {
        cout<<"You are playing the game!"
    }
    int loadgame()
    {
        cout<<"The game is loading!"
    }
    int playmultiplayer()
    {
        cout<<"You are playing multiplayer!"
    }
    Respectfully,
    David.
    - http://www.civilwarsearch.com/ - Civil War Search Directory.
    - http://www.dhq.nu/hutsell/ - Four Free Computer Wargames.
    - http://www.debaunart.com/ - Original and Print Watercolor Artwork.

  6. #6
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    367
    A function can not return a void type, because a void type indicates to the compiler that the function does not return anything. So to make one of your functions void do this:

    void playmultiplayer()
    Be a leader and not a follower.

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Right now, you're expected to be returning an integer from your functions, because that is what you have specified with the return type:
    Code:
    int playgame();
    This implies that you will have a return statement inside your function that returns an integer:
    Code:
    int playgame()
    {
        cout<<"You are playing the game!"
        return 1; // For example.
    }
    Now, it is unlikely in this case that you could return a meaningful value from your function, so it is better to return nothing at all. To do this, specify the return type as void, and either omit the return statement, or have an empty return statement.
    Code:
    void playgame();
    ...
    void playgame()
    {
        cout<<"You are playing the game!"
        // No return statement.
    }
    ...or...
    void playgame()
    {
        cout<<"You are playing the game!"
        return; // Empty return statement.
    }
    Hope this makes things clearer.

    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  8. #8
    Registered User
    Join Date
    Dec 2003
    Posts
    18
    Thanks for helping me clear that up. I also discovered I had one other error. If you look in my code you will notice at several points I was missing my trailing ;.
    Respectfully,
    David.
    - http://www.civilwarsearch.com/ - Civil War Search Directory.
    - http://www.dhq.nu/hutsell/ - Four Free Computer Wargames.
    - http://www.debaunart.com/ - Original and Print Watercolor Artwork.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is it legal to have functions within functions?
    By Programmer_P in forum C++ Programming
    Replies: 13
    Last Post: 05-25-2009, 11:21 PM
  2. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM
  5. Defining Class memeber functions
    By silk.odyssey in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2004, 05:50 PM