gpt4 book ai didi

c - scanf 要求两个值而不是一个

转载 作者:太空狗 更新时间:2023-10-29 15:52:08 24 4
gpt4 key购买 nike

<分区>

当我编译这段代码时,当我选择选项 A 时,它会导致 scanf 两次询问一个值。我在这里缺少什么?

这不是第一次遇到这种情况,所以我怀疑我没有掌握 scanf 的一些相当基础的东西。

#include <stdio.h>
#include <stdlib.h>

int main()
{

char choice;
printf("1. 'enter 'A' for this choice\n");
printf("2. 'enter 'B' for this choice\n");
printf("3. 'enter 'C' for this choice\n");

scanf("%c", &choice);

switch (choice)
{
case 'A':
{
int a =0;
printf("you've chosen menu A\n");
printf("enter a number\n");
scanf("%d\n", &a);
printf("%d\n", a);
}
break;
case 'B':
printf("you've chosen B\n");
break;
case 'C':
printf("you've chosen C\n");
break;
default:
printf("your choice is invalid\n!");
break;
}

return 0;

}

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