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

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

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 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