Thread: Switch function problem

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    12

    Switch function problem

    Hi all

    I've only just started using the C++ language recently and I'm stuck on something which should be pretty easy but I can't solve.

    option is defined as char option[80]. My main problem is that if I enter an option of 9, "PLEASE TRY AGAIN" comes up. I want this to happen if say 22 is entered as well but currently Case 2 is selected, not the default. Why is it doing this and how would I sort it?

    Code:
    do {
              cout << "\n     Products\n";
              cout << "       1.  New\n";
              cout << "       2.  Old\n";
              cout << "       3.  Discontinued\n";
              cout << "       q.  End menu\n";
              cout << "\n\n";
              cout << "\n     Press appropriate key to select > ";
         
         gets(option);	
         
          switch (option[0]) {
                   case '1':
                             new(items);
                             key();
                             break;
                   case '2':
                             old(items);
                             key();
                             break;
                   case '3': 
                             discontinued(items);
                             key();
                             break;
                   case 'q':
                             cout <<("\n\t\t Returning to main.\n");
                             key();
                             break;
                   default:
                             cout <<("\t\t PLEASE TRY AGAIN\n");
                             key();
                             break;
                                      }    //end switch
                      }//end do
    
                             while (option[0] != 'q'  );
                             break;
    Last edited by Jack-Bauer; 05-14-2006 at 10:45 AM. Reason: spelling

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM