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

104
Views
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 answers
Answer question

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