Thread: Outputting a menu

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

    Outputting a menu

    This program is supposed to be a menu system for holiday destinations. As you can see, no menu is displayed, but the user simply has to press a letter and the name of the country is displayed as destination.
    So, could anyone give me a better way to output my program?


    Code:
    //---------------------------------------------------------------------------
    #include<conio>
    #include<iostream>
    #include<cstring>
    #pragma hdrstop
    using namespace std;
    
    //---------------------------------------------------------------------------
    
    #pragma argsused
    int main(int argc, char* argv[])
    {
       char choice;
    
       while ( ( choice = cin.get() ) != 'q' )
       {
       cin.ignore();
       switch ( choice )
    
       {
       case 'a':
    
       cout << "Chosen destination is Australia" << endl;
       break;
    
       case 'u':
    
       cout << "Chosen destination is United States" << endl;
       break;
    
       case 't':
    
       cout << "Chosen destination is Thailand" << endl;
       break;
    
       case 'f':
    
       cout << "Chosen destination is France" << endl;
       break;
    
       case 'g':
       cout << "Chosen destination is Germany" << endl;
       break;
    
       default:
       cout << "Invalid Choice" ;
       }
       }
    
       return 0;
    }
    //---------------------------------------------------------------------------

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    There's probably better ways with iterators etc, but here's one attempt:
    Code:
    #include <iostream>
    
    using namespace std;
    
    const string Options[] = 
    {
      "Destination 1",
      "Destination 2",
      "Destination 3",
      "Destination 4"
    };
    
    int main(void)
    {
      
      for (int i = 0; i < sizeof(Options)/sizeof(Options[0]); i++)
      {
        cout <<Options[i] <<endl;
      }
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    i dont think class string supports the '<<' operator for cout

    so either add this:
    Code:
    ostream& operator<<(ostream& out, string& str)
    {
        out<<str.c_str();
        return out;
    }
    or when u cout the string do
    Code:
        cout <<Options[i].c_str()<<endl;
    -LC
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Originally posted by Lynux-Penguin
    i dont think class string supports the '<<' operator for cout
    Actually, I think '<<' works fine with string. It worked for me in MSVC 6.0. If you use the deprecated header then you will get a compile error, but if you use <iostream> like Hammer did in his example it should work fine.

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    cout << is most deffinatly overloaded for std::string's. It would be silly not to.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>i dont think class string supports the '<<' operator for cout
    Are you seriously suggesting I'd post an answer using code that doesn't compile
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Originally posted by Hammer
    Are you seriously suggesting I'd post an answer using code that doesn't compile
    That's funny, especially considering the posted code doesn't compile . . . (requires #include <string> for me)

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