Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

125
Views
Switch statement with integer value

I am new to C++ and I am stuck on the switch statement because it doesn't seem to give an output when the value in the parentheses is an integer (Console-Program ended with exit code: 0). Although, the same code works fine when I change the type to char. Thank You.

int main()
{
    int num1;          // argument of switch statement must be a char or int or enum
    
    cout<< "enter either 0 or 1" << "\n";
    cin>> num1;
    
    switch (num1)
    {
        case '0':
            cout<< "you entered 0" << endl << endl;
            break;
            
        case '1':
            cout<< "you entered 1" << endl << endl;
            break;
            
    }
    
}
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You are switching on an int, which is correct, but your cases are not integers - they are char since they are surrounded in '.

'0' is never equal to 0, nor is '1' ever equal to 1.

Change the case values to integers.

int main()
{
    int num1;
    
    cout<< "enter either 0 or 1" << "\n";
    cin>> num1;
    
    switch (num1)
    {
        case 0:
            cout<< "you entered 0" << endl << endl;
            break;
            
        case 1:
            cout<< "you entered 1" << endl << endl;
            break;
            
    }
    
}
over 4 years ago · Santiago Trujillo Report

0

int main()
{
    int num1;          // argument of switch statement must be a char or int or enum
    
    cout<< "enter either 0 or 1" << "\n";
    cin>> num1;
    
    switch (num1)
    {
        case 0:
            cout<< "you entered 0" << endl << endl;
            break;
            
        case 1:
            cout<< "you entered 1" << endl << endl;
            break;       
    }

}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!