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

105
Visualizações
Basic C - Switch cases duplication

I am working on a project, where you answer a char, and if it is not one of the 4 answers, it tells you to try again. If it is not the correct one of the 4, it stops running. Here is a snippit.

#include <stdio.h>
#include <stdbool.h>
bool switchCheck = true;
char answer;

        printf("A. English\nB. French\nC. Spanish\nD. German\n");
      do{
        scanf("%c\n", &answer);
        switch (answer){
          case 'C':
            printf("Very nice ");
            break;
          case 'B':
          case 'A':
          case 'D':
            printf("Sorry! Incorrect. Code Ends\n");
            switchCheck =false;
            break;
          default:
            printf("Try Again\n");
        }
      }while(switchCheck==true);
     

For some reason, when I input A, B, or D, it first prints the result for default, and if I do it again immediately afterward, it gives me the right input. Any help?

Thanks!

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You are using scanf like

scanf("%c\n", &answer);

Instead use

scanf( " %c", &answer );

See the blank before the conversion specifier %c. It allows to skip white spaces.

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