scanf_s无法正常工作是什么错误?任何人都可以解决这个问题

时间:2016-05-23 07:41:50

标签: c input scanf getchar

#include<stdio.h>
int main(void)
{
    int sum(), sub(), mul(), div();
    char ans;
    printf("\ta) Add\n");
    printf("\tb) Sub\n");
    printf("\tc) Multiply\n");
    printf("\td) Divition\n");
    scanf_s("%c",&ans);
    printf("\nYour answer is %c",ans);
    if((ans == 'A')||(ans == 'a'))
        printf("The sum of Two number is %d\n",sum());
    else if((ans == 'B')||(ans == 'b'))
        printf("The subraction of Two number is %d\n",sub());
    else if((ans == 'C')||(ans == 'c'))
        printf("The product of two number is %d\n",mul());
    else if((ans == 'D')||(ans == 'd'))
        printf("The divition of two number is %d\n",div());
    else
        printf("Enter the valid option\n");
}
int sum()
{
    int x,y;
    printf(" Addition\n");
    printf("Enter the first number = ");
    scanf_s("%d",&x);
    printf("Enter the second number = ");
    scanf_s("%d",&y);
    return x+y;
}
int sub()
{
    int x,y;
    printf(" Subraction\n");
    printf("Enter the first number = ");
    scanf_s("%d",&x);
    printf("Enter the second number = ");
    scanf_s("%d",&y);
    return x-y;
}
int mul()
{
    int x,y;
    printf(" Mltification\n");
    printf("Enter the first number = ");
    scanf_s("%d",&x);
    printf("Enter the second number = ");
    scanf_s("%d",&y);
    return x*y;
}
int div()
{
    int x,y;
    printf(" Divition\n");
    printf("Enter the first number = ");
    scanf_s("%d",&x);
    printf("ENter the second number = ");
    scanf_s("%d",&y);
    return x/y;
}

scanf_s("%c",&ans);

函数scanf无法识别我的输入

代码中的错误

我用的时候 ans=getchar(); 工作得很完美但是...... scanf无法识别我输入的内容

任何人都可以使用visual studio 2010解释代码中的问题

1 个答案:

答案 0 :(得分:0)

要扫描单个字符,您必须在%c之前放置一个空格:

scanf(" %c", &char);

需要空格来消耗输入缓冲区中的任何\n字符。您必须将空格仅用于char类型。