gpt4 book ai didi

c - 每当我执行该程序时,它最后都会接受不需要的额外输入

转载 作者:行者123 更新时间:2023-11-30 18:30:39 26 4
gpt4 key购买 nike

#include<stdio.h>
int main()
{
int i;
char name[3];
float price[3];
int pages[3];

printf("Enter names, price and no of pages of 3 books:\n ");
fflush(stdin);
for(i=0;i<=2;i++)
scanf(" %c%f%d\n",&name[i],&price[i],&pages[i]);

printf("And this is what you have entered:\n ");
for(i=0;i<=2;i++)
printf(" %c %f %d \n",name[i],price[i],pages[i]);

return 0;
}

最佳答案

scanf 中删除 \n

  scanf(" %c%f%d\n",&name[i],&price[i],&pages[i]);    
// ^^ Remove it.

scanf 中使用 \n,按 Enter 时,scanf 将跳过 \n 传递到输入缓冲区并期望非 \n 字符停止从输入缓冲区读取。

关于c - 每当我执行该程序时,它最后都会接受不需要的额外输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29492839/

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