Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

120
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda