【问题标题】:I'm not able to use scanf() in else statment我无法在 else 语句中使用 scanf()
【发布时间】:2015-02-04 13:38:39
【问题描述】:

我们可以在 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(3) 的结果,它给出了成功扫描的项目数
  • 顺便说一句,不要忘记编译所有警告和调试信息(例如gcc -Wall -Wextra -g)然后使用调试器gdb

标签: c if-statement scanf


【解决方案1】:

当你在这个scanf的第一次读取中输入数据时:

scanf("%c", &ms);

换行符保留在键盘中。为了解决这个问题,在你的第二个 scanf 中放置一个空格:

scanf(" %c",&s);

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

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

【讨论】:

  • 谢谢。我还有一个疑问。为什么使用 int main() 而不是 void main()。我对 return(0) 感到困惑,并且我多次搜索 return 但仍然没有得到完美的答案。同样在这里为什么我们在这里使用 return(0) ?
  • @SharadGaur - 不仅有不同的操作系统架构,还有硬件平台架构。因此,为了保持编写代码的标准化方式,CC++ 由 ISO 标准化。他们要求main 应该返回int
【解决方案2】:

输入年龄值时忘记添加&amp;,可以使用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;
}

另外,int main()return 0; 更好。

【讨论】:

  • 缓冲区中的换行符呢?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-20
  • 2022-11-15
  • 2017-03-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多