Thread: is there a better/shorter way to do a menu

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    6

    is there a better/shorter way to do a menu

    I need to write a generic menu that can be easily modified for future use. This is what I have. It works and is bug free that I can tell but is there a better way to do this. Or easier way? Please offer suggestion and example code only. I am new to c/c++ and would like to do this on my own, just need dirrection.


    //Generic menu.

    #include <iostream.h>
    #include <stdlib.h>
    #include <string.h>

    void read_data();
    void write_data();

    int main()
    { //start main()

    //-------------------------------------- initializations ---------------------
    int compare1, compare2, compare3;
    const int max = 20;
    char answer_str[max];
    //----------------------------------------------------------------------------

    while((compare1 != 0) && (compare2 != 0) && (compare3 != 0))
    { //start while loop

    system("cls");

    //-------------------------------------- create menu -------------------------
    cout<<"1 - Enter new data."<<endl;
    cout<<"2 - View existing data."<<endl;
    cout<<"exit"<<endl<<endl;
    //----------------------------------------------------------------------------

    cin.getline(answer_str, max); //reads answer_str as a string of char data

    //-------------------------------------- compare answer_str to a constant ----
    compare1 = strcmp(answer_str, "1");
    compare2 = strcmp(answer_str, "2");
    compare3 = strcmp(answer_str, "exit");
    //----------------------------------------------------------------------------

    //-------------------------------------- print the result of strcmp ----------
    //cout<<"compare1 = "<<compare1<<endl; for troubleshooting
    //cout<<"compare2 = "<<compare2<<endl;
    //cout<<"compare3 = "<<compare3<<endl;
    //----------------------------------------------------------------------------

    } // end while loop

    //-------------------------------------- deside what to do -------------------
    if(compare1 == 0) write_data();
    if(compare2 == 0) read_data();
    if(compare3 == 0) return 0;
    //----------------------------------------------------------------------------

    return 0;

    } //end main

    //-----------------------------------------------------------------------------

    void read_data()
    {
    cout<<"read some data"<<endl;
    system("pause");
    main();
    }

    //-----------------------------------------------------------------------------

    void write_data()
    {
    cout<<"write some data"<<endl;
    system("pause");
    main();
    }

    thanks,
    sizzle_chest

  2. #2
    Unregistered
    Guest
    int choice;
    char continue = 'y';

    while(continue == 'y')
    cout select from choices below:
    cout 1) add
    cout 2)delete
    cout 3)view
    cotu 4)exit

    cin choice

    switch(choice)
    case 1:
    add();
    break;
    case 2:
    delete();
    break;
    case 3:
    view();
    break;
    case 4:
    continue = 'n';
    break;
    default:
    cout must choose value from 1-4 inclusive
    break;
    //end switch
    //end while loop
    //continue program if desired

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    6
    I need it to loop if the user does not put in the correct input. With the switch case it goes into an infinite loop when a non numeric charicter is entered. which is why I wrote it the way i did.
    The only way i could think to do it was with strcmp().

  4. #4
    Unregistered
    Guest
    the default will handle all ints outside the range.

    if you want you can error check off of cin choice with either the string entry method or cin.fail() cin.clear() combination.

    error checking is completely separate from creating a menu however.

    //whatever goes ahead of user input

    char okInput = 'n';
    while(okInput == 'n')
    {
    cin >> choice;

    if(cin.fail())
    {
    cout << "error in selection. Must use an integer as input" << endl;
    cin.clear();
    }
    else
    okInput = 'y';
    }

    switch (choice)
    //etc

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Replies: 2
    Last Post: 03-05-2009, 10:25 AM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Constructive Feed Back (Java Program)
    By xddxogm3 in forum Tech Board
    Replies: 12
    Last Post: 10-10-2004, 03:41 AM