gpt4 book ai didi

c - scanf_s 不起作用,错误是什么?任何人都可以解决这个问题吗

转载 作者:行者123 更新时间:2023-11-30 19:38:55 27 4
gpt4 key购买 nike

#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 的代码中的问题

最佳答案

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

scanf(" %c", &char);

需要空白空间来消耗输入缓冲区中的任何 \n 字符。您必须仅为 char 类型添加空格。

关于c - scanf_s 不起作用,错误是什么?任何人都可以解决这个问题吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37385262/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com