0

can we use scanf() function in else , like i used in this code. Bec i'm not able to enter value(char) for sex variable. so i want to why i'm not able to enter the value for sex variable ?

#include<stdio.h>
#include<conio.h>

void main()
{
    clrscr();

    int age;
    char s,ms;

    printf("Please enter M if you are married or U if you are un-married\n");
    scanf("%c", &ms);

    if(ms=='M')
        printf("\nyou are recruted");
    else if(ms=='U')
    {
        printf("\nenter sex- A for male & B for female\n");
        scanf("%c",&s);

        if(s=='A')
        {
            printf("\nEnter your age\n");
            scanf("%d",age);

            if(age>30)
                printf("\nYou are selected");
            else
                printf("\nYour age is less for this job");
        }
        else if(s=='B')
        {
            printf("\nEnter your age\n");
            scanf("%d",age);

            if(age>25)
                printf("\nyou are recruted");
            else
                printf("\nyour age is less to be recruted");
        }
        else
        {
            printf("Please enter A for male or B for female");
        }
    }
    else
    {
        printf("PLEASE ENTER THE CORRECT VALUE\n please enter M for Married or U for un-married");
    }
    getch();
}

Output :

Please enter M if you are married or U if you are un-married
U
enter sex A for male or B for female
Please enter A for male or B for female
CiaPan
  • 9,381
  • 2
  • 21
  • 35
Sharad Gaur
  • 151
  • 1
  • 13
  • 1
    You should always test the result of [scanf(3)](http://man7.org/linux/man-pages/man3/scanf.3.html) which gives the number of *successfully scanned items* – Basile Starynkevitch Feb 04 '15 at 13:43
  • BTW, don't forget to compile with all warnings & debug info (e.g. `gcc -Wall -Wextra -g`) then **use the debugger** (`gdb`) – Basile Starynkevitch Feb 04 '15 at 13:54
  • possible duplicate of [Scanf skips every other while loop in C](http://stackoverflow.com/questions/1669821/scanf-skips-every-other-while-loop-in-c) – Werner Henze Feb 04 '15 at 14:25

2 Answers2

3

When you enter the data in the first read of this scanf:

scanf("%c", &ms);

A newline character remains in the keyboard. To solve this put a space in your second scanf:

scanf(" %c",&s);

This is to consume any trailing character in the stdin that might have been left by previous user input (like the carriage return), before the scanf reads the user input. Also note that you missed the & in scanf("%d",age);:

Also note main should return int.

#include <stdio.h>
#include <stdlib.h>

int main()    
{   
    int age;    
    char s,ms;    
    printf("Please enter M if you are married or U if you are un-married\n");    
    scanf("%c", &ms);        
    if(ms=='M')    
        printf("\nyou are recruted");    
    else if(ms=='U')  
    {    
        printf("\nenter sex- A for male & B for female\n");
        scanf(" %c",&s);    
        if(s=='A')   
        {    
            printf("\nEnter your age\n");    
            scanf("%d",&age);    
            if(age>30)    
                printf("\nYou are selected");    
            else    
                printf("\nYour age is less for this job");    
        }    
        else if(s=='B')    
        {    
            printf("\nEnter your age\n");    
            scanf("%d",&age);    
            if(age>25)    
                printf("\nyou are recruted");    
            else    
                printf("\nyour age is less to be recruted");    
        }    
        else    
        {    
            printf("Please enter A for male or B for female");    
        }    
    }
    else    
    {    
        printf("PLEASE ENTER THE CORRECT VALUE\n please enter M for Married or U for un-married");    
    }    
    getchar();    
    return 0;
}

Read Why is adding a leading space in a scanf format string recommended?

Community
  • 1
  • 1
Sadique
  • 22,572
  • 7
  • 65
  • 91
  • Thankyou. I have one more doubt. Why int main() instead of void main(). I'm confused with return(0) , and i searched about return many times but still not getting the perfect answer. same here why are we using return(0) here ? – Sharad Gaur Feb 04 '15 at 15:00
  • @SharadGaur - There are not only operating systems' architecture which vary but also hardware platform architecture. Hence to keep a standardized way of writing code `C` and `C++` were standardized by ISO. They mandate that `main` should return `int` – Sadique Feb 05 '15 at 04:34
0

You forget to add a & when input the age value and you can use getchar() to deal the new line in buffer.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int age;
    char s,ms;
    printf("Please enter M if you are married or U if you are un-marriedn");
    scanf("%c", &ms);
    getchar();       // deal with thre newline in buffer
    if(ms=='M')
    printf("nyou are recruted");
    else if(ms=='U')
    {
        printf("nenter sex- A for male & B for femalen");
        scanf("%c",&s);
        getchar();    // deal with thre newline in buffer
        if(s=='A')
        {
            printf("nEnter your agen");
            scanf("%d",&age);    // add & when you use scanf
            if(age>30)
            printf("nYou are selected");
            else
            printf("nYour age is less for this job");
        }
        else if(s=='B')
        {
            printf("nEnter your agen");
            scanf("%d",&age); // add & when you use scanf
            if(age>25)
            printf("nyou are recruted");
            else
            printf("nyour age is less to be recruted");
        }
        else
        {
            printf("Please enter A for male or B for female");
        }
    }
    else
    {
        printf("PLEASE ENTER THE CORRECT VALUEn please enter M for Married or U for un-married");
    }
    getchar();
    return 0;
}

Besides, int main() and return 0; is better.

lqhcpsgbl
  • 3,694
  • 3
  • 21
  • 30