help with switch statement

This is a discussion on help with switch statement within the C++ Programming forums, part of the General Programming Boards category; Why doesn't the following code work? Code: #include <iostream> using namespace std; int main() { char switchstart; cout << "Enter ...

  1. #1
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357

    Question help with switch statement

    Why doesn't the following code work?

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    
    {
    	char switchstart;
    
    	cout << "Enter your desired color" << endl;
    
    	enum{black = 0, blue = 1, green = 2, red = 4, purple = 5, yellow = 6, white = 7, gray = 8};
    
    	for ( ; ; )
    	{
    	
    		cin >> switchstart;
    	
    		switch (switchstart)
    	{
    	case black:
    		system ("color 0F");
    		break;
    
    	case blue:
    		system ("color 1F");
    		break;
    
    	case green:
    		system ("color 2F");
    		break;
    
    	case red:
    		system ("color 4F");
    		break;
    
    	case purple:
    		system ("color 5F");
    		break;
    
    	case yellow:
    		system ("color 60");
    		break;
    
    	case white:
    		system ("color 70");
    		break;
    
    	case gray:
    		system ("color 8F");
    		break;
    	}
    	}
    
    
    	return 0;
    
    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Because 1 isn't the same as '1'.

    Make this:
    char switchstart;
    into this:
    int switchstart;
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    I want to type in the word instead of the number. Should I use one of those strange functions like getchar or something instead of cin?

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    31
    I don't claim to be any good at C++, but I believe that you have to have an array, not a char. I think a char will only store one keystroke, whereas an array can store many characters. I think.

  5. #5
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Unfortunately you can't use enums like that. Enums are only for code readability really, not for inputing and outputing. And you can only use int values or equivalent in a switch command. If you want the user to write the word, then the simplest solution would be to use if statements (or a lookup array table):
    Code:
    string choice;
    cout<<"Choose color: ";
    cin<<choice;
    
    if (choice=="black")
      // black
    else if (choice=="red")
      // red
    ...

  6. #6
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    isnt it also true that switch statments only work with integers and chars.......? and for the famous WIN32 API

    Code:
    switch(message)
    {
    case WM_CREATE:
    ....
    }
    message is unsigned int...and the window messages are unsigned int...i think?
    nextus, the samurai warrior

  7. #7
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by nextus
    isnt it also true that switch statments only work with integers and chars.......?
    not true. switch statements work with any integral types (IE enum datatypes).

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>Unfortunately you can't use enums like that
    Why not:
    Code:
    #include <iostream>
    
    using std::cin;
    using std::cout;
    using std::endl;
    
    int main()
    {
      enum COLOURS {black, blue, red, invalid};
      int i = invalid;
      
      cout <<"Enter number (0 to 2):" <<endl;
      cin >>i;
      
      switch (i)
      {
        case black: cout <<"black"; break;
        case blue : cout <<"black"; break;
        case red  : cout <<"black"; break;
        default   : cout <<"Wrong"; break;
      }
      
      cout <<endl;
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by Hammer
    >>Unfortunately you can't use enums like that
    Why not
    because he wants the user to be able to type in the name of the color, not its numerical equivalent.

    volk, you have to set up a bunch of strings and compare the input data with those and base your results on that. All enum does is create a bunch of constants (and new integral datatypes). You can't compare the names of those constants just like you can't compare the names of variables in your program.

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>because he wants the user to be able to type in the name of the color, not its numerical equivalent.
    Doh! I missed that part!

    Anyway, something like this might do for you:
    Code:
    string Colours[] = {"Black", "Blue", "Red"};
    string s;
      
    cout <<"Enter a colour:" <<endl;
    cin >>s;
    
    for (int i = 0; i < sizeof (Colours) / sizeof (Colours[0]); i++)
    {
      if (Colours[i] == s)
      {
          // Found a match
      }
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutli Switch statement help
    By elwad in forum C Programming
    Replies: 9
    Last Post: 05-09-2009, 03:19 AM
  2. Switch statement / default:
    By kcpilot in forum C Programming
    Replies: 4
    Last Post: 12-02-2008, 02:14 PM
  3. switch statement
    By guillermoh in forum C Programming
    Replies: 5
    Last Post: 03-10-2008, 02:17 PM
  4. char switch statement
    By jmarsh56 in forum C++ Programming
    Replies: 7
    Last Post: 05-03-2006, 05:04 PM
  5. Efficiency with the switch() statement...
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2001, 02:47 PM

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