Thread: new question: understanding parse error

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

    new question: understanding parse error

    I normally program in PHP, but I'm starting to learn C++ as well. Most of this is making sense, but I am getting a parse error when compiling that doesn't make sense to me. My goal is to create a simple program that:
    1. displays a menu, and ask for a choice
    2. displays text based on the answer, and then goes back to #1.

    I compared my code against the tuts I'm reading, and everything looks fine (though it obviously isn't).

    Here is the compiler log:
    Compiler: Default compiler
    Executing g++.exe...
    g++.exe "C:\[c++]\pig.cpp" -o "C:\[c++]\pig.exe" -g3 -I"C:\Dev-Cpp\include\c++" -I"C:\Dev-Cpp\include\c++\mingw32" -I"C:\Dev-Cpp\include\c++\backward" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib"
    C:/[c++]/pig.cpp: In function `int main()':
    C:/[c++]/pig.cpp:9: parse error before `{' token
    C:/[c++]/pig.cpp:17: `display_answer' undeclared (first use this function)
    C:/[c++]/pig.cpp:17: (Each undeclared identifier is reported only once for each
    function it appears in.)
    C:/[c++]/pig.cpp: In function `int display_answer(int)':
    C:/[c++]/pig.cpp:20: `int display_answer(int)' used prior to declaration
    C:/[c++]/pig.cpp:25: `display_menu' undeclared (first use this function)
    C:/[c++]/pig.cpp: At global scope:
    C:/[c++]/pig.cpp:37: ISO C++ forbids declaration of `display_menu' with no type
    C:/[c++]/pig.cpp:37: `int display_menu()' used prior to declaration
    C:/[c++]/pig.cpp:38: parse error before `return'
    Execution terminated

    Here is the code:
    Code:
    #include <iostream>
    using namespace std;
    int display_answer(int choice);
    
    int main()
    {
        cout << "Let's just get this damn thing to compile before calling the function.\n";
        int display_menu()
        {
            cout << "What do you want to know?\n";
            cout << "(choose a number)\n\n";
            cout << "\t[1] Boxers or briefs?\n";
            cout << "\t[2] How pretty are you?\n";
            cout << "\t[3] Can I touch you?\n";
            int choice;
            cin>>choice;
            display_answer(choice);
        }
        int display_answer(int choice)
        {
            switch(choice)
            {
                case 1:
                    cout << "Boxer-breifs.\n";
                    display_menu();
                    break;
                case 2:
                    cout << "7.2\n";
                    display_menu();
                    break;
                case 3:
                    cout << "No.\n";
                    display_menu();
                    break;
            }
        }
        display_menu();
        return 0;
    }
    I figured start at the top, which is the parse error for
    int display_menu()
    {

    Can someone please enlighten me? TIA

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Functions do not nest. Move the definition of display_answer and display_menu outside of main.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    2
    Ahah! Thanks much.

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    Also note that you have infinate mutual recursion going on. While this is valid and clever in fancy functional proramming langauges, we don't do such things here in C++, here we are programming a computer, not solving lamda calculus. The C++ version would look something like this
    Code:
    int display_menu()
    {
    //...  as written
        int choice=0;
        cin>>choice;
        return choice;
    }
    
    void display_choice(int choice) 
    {
    //.. as written, but no display_menu()
    //.. no return 0; void functions do not need
    // .. a return, you can have one, but it's just
    // return; by itself.
    }
    
    int main() {
        for(int choice=display_menu(); choice != 0; choice = display_menu()) display_choice(choice);
        return 0;
    }
    The changes to the display_menu() have a mild amount of trickery. Here we rely on the fact that if a stream encounters bad input it does not change the value it's trying to write to. In our case if the user enters 'Q' in responce to the menu then display_menu() returns zero. We also have the side effects that cin.good() is false and we still have a 'Q' sitting in the input buffer, but we don't care about that right now. Stream driven menus are generally awkward to work with.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. Avoiding Global variables
    By csonx_p in forum Windows Programming
    Replies: 32
    Last Post: 05-19-2008, 12:17 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM