Switch in C+

This is a discussion on Switch in C+ within the C++ Programming forums, part of the General Programming Boards category; Hey everybody! I couldnt use the switch in C++. My study code is: Code: #include <iostream> using std::cout; using std::cin; ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    10

    Switch in C+

    Hey everybody!

    I couldnt use the switch in C++.
    My study code is:
    Code:
    #include <iostream>
    using std::cout;
    using std::cin;
    using std::endl;
    
    int main(){
        int a; 
        cin >> a;
        
               switch (a) {
                      case '1':
                      cout << "111";           
                      break;
                      
                      case '2':
                      cout << "222";                  
                      break;
                      
                      case '3':
                      cout << "333";                 
                      break;
                      
                      default:
                      cout << "Incorrect";
    
                      break;
               }
       
     system("PAUSE");
     return EXIT_SUCCESS;
    Where the error in my code? How can I fix it?

    Thanks!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,888
    '1' is a char whose value in ASCII is 49. a is an int, so unless you enter 49 as the input, you are unlikely to get "111" printed. In short, change '1' to 1, '2' to 2, and '3' to 3.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    10
    Thanks!
    I understood now.

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,248
    Quote Originally Posted by laserlight View Post
    '1' is a char whose value in ASCII is 49. a is an int, so unless you enter 49 as the input, you are unlikely to get "111" printed. In short, change '1' to 1, '2' to 2, and '3' to 3.
    Or change the variable a to be of char type.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  2. ascii rpg help
    By aaron11193 in forum C Programming
    Replies: 18
    Last Post: 10-29-2006, 01:45 AM
  3. Switch
    By cogeek in forum C Programming
    Replies: 4
    Last Post: 12-23-2004, 05:40 PM
  4. Switch Case
    By FromHolland in forum C++ Programming
    Replies: 7
    Last Post: 06-13-2003, 03:51 AM

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