Functions for menus

This is a discussion on Functions for menus within the C++ Programming forums, part of the General Programming Boards category; I am now learning to create functions. I have read the tutorial on this site and several others. I have ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    25

    Functions for menus

    I am now learning to create functions. I have read the tutorial on this site and several others. I have not been able to find any guidance though as to how to creat a function that returns a menu. The menu is basic:

    Code:
    cout << "************************" << endl; //menu for options
    cout << "* A - Addition         *" << endl;
    cout << "* B - Subtraction      *" << endl;
    cout << "* C - Multiplication   *" << endl;
    cout << "* D - Division         *" << endl;
    cout << "* E - Random problems  *" << endl;
    cout << "* F - Quit             *" << endl; 
    cout << "************************" << endl;

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    well, to be formal (and picky), theres no such thing as a 'menu' in the (standard) C++ library.

    im assuming you mean you want a function that will print a menu to the console and get some user input?

    adding to your code:
    Code:
    void displayMenu()
    {
       // define a variable to store the users single character response here
    
       cout << "************************" << endl; //menu for options
       cout << "* A - Addition         *" << endl;
       cout << "* B - Subtraction      *" << endl;
       cout << "* C - Multiplication   *" << endl;
       cout << "* D - Division         *" << endl;
       cout << "* E - Random problems  *" << endl;
       cout << "* F - Quit             *" << endl; 
       cout << "************************" << endl;
       
       // read in the users response
      // return the response
    }
    notice the return type is void, so it wont return anything as you know. if you just want the function to display the menu, then your done, and you can take care of the user input in your calling function.

    however, as youll notice the comments, i think it would be better to include the user input code inside the function (changing the function name to suit the functions task of course). if this is the case, youll need to add in a few things, note my comments, and change the function's signature. more specifically, the functions return type.

    edit:
    and if both of these arent what your looking for, you can create a string with the above as its contents, and return that. this would return a 'menu', i guess. also it would be completely pointless!

    hope it helps.
    Last edited by nadroj; 01-15-2008 at 09:21 PM.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    Well the easiest thing to do would be to just put the above code in a function:
    Code:
    void printMenu(){
        cout << "************************" << endl; //menu for options
        cout << "* A - Addition         *" << endl;
        cout << "* B - Subtraction      *" << endl;
        cout << "* C - Multiplication   *" << endl;
        cout << "* D - Division         *" << endl;
        cout << "* E - Random problems  *" << endl;
        cout << "* F - Quit             *" << endl; 
        cout << "************************" << endl;
    }
    That won't return the menu, but it will display it whenever you call the fucntion.

    You can also make the menu text a constant string that the function returns:
    Code:
    const char *menu(){
      static const char *const menuText = 
        "************************\n"
        "* A - Addition         *\n"
        "* B - Subtraction      *\n"
        "* C - Multiplication   *\n"
        "* D - Division         *\n"
        "* E - Random problems  *\n"
        "* F - Quit             *\n"
        "************************\n";
      return menuText;
    }
    Here you can use string instead of char *, although in this particular case char * works just as well. Also, note that writing adjacent strings like that is equivalent to writing one long string.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    25
    Thanks for the help guys....I apologize for the incorrect terminology...i'm still learning the language as you can see.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    no need to apologize! i was just being picky remember. hope it works out

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    25
    So here's what I tried...which is basically what you both had said:

    Code:
    void get_menu();
    
    int main()
    {
    void get_menu();
    
    cout << " Please choose an option from the menu above.  ";
    cin >> protype;
    }
    
    void getmenu()
    {
       cout << "************************" << endl; //menu for options
       cout << "* A - Addition         *" << endl;
       cout << "* B - Subtraction      *" << endl;
       cout << "* C - Multiplication   *" << endl;
       cout << "* D - Division         *" << endl;
       cout << "* E - Random problems  *" << endl;   
       cout << "* F - Quit             *" << endl; 
       cout << "************************" << endl;
    }
    I'm really just looking for a calling function to display the menu. However when it compiles and runs nothing comes up.
    Last edited by got1sleeve; 01-15-2008 at 09:58 PM. Reason: sytanx

  7. #7
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    your first statement inside your main isnt doing what you think it is. remove the void inside main, so its just get_menu();

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    25
    Quote Originally Posted by nadroj View Post
    your first statement inside your main isnt doing what you think it is. remove the void inside main, so its just get_menu();
    If i removed the void from inside main I get a compile error:
    Code:
    Lab 3.obj : error LNK2001: unresolved external symbol "void __cdecl get_menu(void)" (?get_menu@@YAXXZ)
    C:\Users\Josh\Documents\Visual Studio 2005\Projects\Lab 3\Debug\Lab 3.exe : fatal error LNK1120: 1 unresolved externals

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You call get_menu and have declared a get_menu function but implemented a getmenu function.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,820
    You need to learn how to interpret linking errors.
    "unresolved external symbol" means the linker was unable to find a function or a variable.
    "void __cdecl get_menu(void)" says it couldn't find "void get_menu()". So where in your code is get_menu? I don't see it, do you? All I see it getmenu!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Jan 2008
    Posts
    25
    Quote Originally Posted by Elysia View Post
    You need to learn how to interpret linking errors.
    "unresolved external symbol" means the linker was unable to find a function or a variable.
    "void __cdecl get_menu(void)" says it couldn't find "void get_menu()". So where in your code is get_menu? I don't see it, do you? All I see it getmenu!
    I'm kind of teaching myself and learning from you all but i see what you're saying about the error that makes sense once its explained haha but to a noob just reading that it looks like swahili

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,820
    I know it does. VC++ is horrible on linking errors, but once you get it explained to you, you can start interpreting them on your own. Good luck on that.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

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