Switch Problem

This is a discussion on Switch Problem within the C++ Programming forums, part of the General Programming Boards category; hmm im testing doing a menu and i do get it work when i use numbers, but as i use ...

  1. #1
    Interested Newbie
    Join Date
    Sep 2004
    Location
    Sweden
    Posts
    51

    Switch Problem

    hmm im testing doing a menu and i do get it work when i use numbers, but as i use it know
    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
    	cout<< "Welcome to my menu test! " << endl;
    	int menu;
    	cout<< "[S]ave" << endl;
    	cout<< "[L]oad" << endl;
    	cout<< "[Q]uit" << endl;
    
    	cin>>menu;
    
    	switch(menu)
    {
    	case S || s:
    	cout<< " Yes S works good";
    	break;
    	case L || l:
    	cout<< " Yes l works good";
    	case q || Q:
    	cout<< " Yes Q works good";
    	default:
    	return 0;
    }
    
    return 0;
    }
    Does it works with others than numbers at all??

  2. #2
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    If you're reading characters, use a char type.

    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
      char menu;
    
      cout<< "Welcome to my menu test! " << endl;
      cout<< "[S]ave" << endl;
      cout<< "[L]oad" << endl;
      cout<< "[Q]uit" << endl;
    
      cin >> menu;
    
      switch(menu)
      {
        case 'S':
        case 's':
          cout<< " Yes S works good";
          break;
    
        case 'L':
        case 'l':
          cout<< " Yes l works good";
          break;
    
        case 'Q':
        case 'q':
          cout<< " Yes Q works good";
          break;
      }
      return 0;
    }
    Last edited by Dante Shamest; 09-16-2004 at 03:34 AM.

  3. #3
    Interested Newbie
    Join Date
    Sep 2004
    Location
    Sweden
    Posts
    51
    Thank you so much, how could i be so stupid! :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Switch statement problem
    By jalex39 in forum C Programming
    Replies: 6
    Last Post: 03-08-2008, 04:05 PM
  2. problem on switch
    By toxicherry in forum C Programming
    Replies: 11
    Last Post: 12-31-2007, 05:17 AM
  3. Replies: 1
    Last Post: 08-31-2004, 05:07 AM
  4. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21