Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

119
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda