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

206
Views
The output does not match with the input (C language)

I am making a simple C program that would ask your age and gender. If your gender is not M or F the program will ask for a different input. If your age is less than zero, the program will require for another input. This is my code:

#include<stdio.h>

int main(){
    
    int a;
    char g;
    
    entry:
    printf("%s","AGE : ");
    scanf("%d", &a);
    getchar();
    
    insert:    
    printf("%s","GENDER :");
    scanf("%s", &g);
    getchar(); 
    
    while (a < 0){
        goto entry;
        }
    
    switch(g){
        
        case 'M':{
            printf("I am a %d yr old male", a);break;
        }
        case 'F':{
            printf("I am a %d yr old female", a);break;
        }
        default:{
            goto insert; break;
        }
    }

    return 0;
}

and this is an example of the program running

AGE : 21

GENDER :M

I am a 0 yr old male

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Altough I detected a weirdness (see the large comment) in code, it outputs as you expected so you can use an approach like this:

#include<stdio.h>
#include<stdbool.h>

int main(){

    int age, a;
    char g;
    bool validInput;

    do {
        validInput = true;

        printf("%s","AGE : ");
        scanf("%d", &age);
        /*
         * If you use directly the 'a' var, somehow it will not print
         * input age. That's why I had to use a = age assignment.
         * This is weird, if someone knows why this happens let me
         * know please.
         */
        a = age;
        if(a < 0) {
            validInput = false;
        }

        printf("%s","GENDER : ");
        scanf("%c", &g);
        getchar();
        if(g != 'M' && g != 'F') {
            validInput &= false;
        }

    } while(!validInput);

    switch(g){

        case 'M':
            printf("I am a %d yr old male\n", a); break;
        case 'F':
            printf("I am a %d yr old female\n", a); break;
        default:
            printf("Oops! Something went wrong...\n");
    }

    return 0;
}
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!