c - 我无法在 else 语句中使用 scanf()

标签 c if-statement scanf

我们可以在 else 中使用 scanf() 函数吗,就像我在这段代码中使用的那样。 因为我无法为性别变量输入值(字符)。 所以我想知道为什么我无法输入性别变量的值?

#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();
}

输出:

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

最佳答案

当您在第一次读取此 scanf 时输入数据时:

scanf("%c", &ms);

换行符保留在键盘中。要解决这个问题,请在您的第二个 scanf 中添加一个空格:

scanf(" %c",&s);

这是在 scanf< 之前消耗 stdin 中可能由先前用户输入(如 carriage return)留下的任何尾随字符 读取用户输入。另请注意,您错过了 scanf("%d",age); 中的 &:

另请注意 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;
}

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

关于c - 我无法在 else 语句中使用 scanf(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28322838/

相关文章:

c - 使用 Intel MKL 计算 `trans(a)*inv(b)*a` 的正确方法

c - 将文件系统用作存储库时处理对齐问题的策略

html - 我应该在哪里安装 webkit 的插件?

python - 我无法正确跟踪 PyGame 中的 KEYDOWN 事件

带有 && 的 C if 语句 - 哪个函数将首先执行?

C 关于PUTCHAR

MYSQL INSERT 基于 3 列的值

c - 删除数据文件中的字符串

c - 从文件读取时遇到问题

c - 忽略所有输入字符,直到输入 'a' - 在 C 中使用 scanf