Thread: Switch in C+

  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
    28,413
    '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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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, 09: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, 06:40 PM
  4. Switch Case
    By FromHolland in forum C++ Programming
    Replies: 7
    Last Post: 06-13-2003, 03:51 AM