简体   繁体   English

我无法在其他陈述中使用scanf()

[英]I'm not able to use scanf() in else statment

can we use scanf() function in else , like i used in this code. 我们可以在else中使用scanf()函数吗,就像我在此代码中使用的那样。 Bec i'm not able to enter value(char) for sex variable. 因为我无法为性别变量输入value(char)。 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

When you enter the data in the first read of this scanf: 在此scanf的第一次读取中输入数据时:

scanf("%c", &ms);

A newline character remains in the keyboard. 换行符保留在键盘中。 To solve this put a space in your second scanf: 要解决此问题,请在第二个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. 这是为了消耗在scanf读取用户输入之前,以前的用户输入(例如carriage return )可能留下的stdin中的任何尾随字符。 Also note that you missed the & in scanf("%d",age); 另外请注意,你错过了&scanf("%d",age); :

Also note main should return int . 还要注意main应该返回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? 阅读Why is adding a leading space in a scanf format string recommended?

You forget to add a & when input the age value and you can use getchar() to deal the new line in buffer. 输入年龄值时,您忘记添加& ,并且可以使用getchar()在缓冲区中处理新行。

#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; 此外, int main()return 0; is better. 更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM