急急急!小问题,帮大忙!

shinomori0692 2003-01-05 01:53:24
下面程序中倒数第七行的scanf("%c",&s);为什么编译器不予理睬,直接跳过???
# include <stdio.h>
# include <conio.h>

main()
{
struct classes{
char name[30];
double price;
double sale;
double income;
}stuff[100];

int select;
int i;
char s;

printf("**************************************\n");
printf("*1. data input 2. data account *\n");
printf("*3. data output 4. quit *\n");
printf("**************************************\n");

scanf("%d",&select);
switch(select)
{
case 1:
for(i=0;i<100;i++)
{
printf("\nPlease input the name: ");
scanf("%s",stuff[i].name);
printf("\nPlease input the price: ");
scanf("%s",stuff[i].price);
printf("\nPlease input the sale:");
scanf("%s",stuff[i].sale);
stuff[i].income=stuff[i].price*stuff[i].sale;
printf("Would you want to creat another stuff?(y/n)");
scanf("%c",&s);
if(s=='n')
break;
}
break;
}
}
...全文
26 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
积木 2003-01-05
  • 打赏
  • 举报
回复
是啊,是啊,我也是,那个scanf让我郁闷了好长的时间呢!
长经验值了
chinajiji 2003-01-05
  • 打赏
  • 举报
回复
flushall()好象不是C标准库函数,它是TC下的专用函数,而fflush()是C标准函数.用标准函数的程序可移植性要好些.
hongfeeling 2003-01-05
  • 打赏
  • 举报
回复
我也遇到过这样的情况。
把那个scanf("%c",&s);
改成:getchar()就可以了。
一直不知其所以然。看了楼上的说法有点明白了。
感谢!
aurora_song 2003-01-05
  • 打赏
  • 举报
回复
可以肯定的是你在倒数第七行的scanf之前还有过其他scanf。SwordMan2001(天笑2001) 说的是对的,看看flushall()或者fflush(stdin)的用法和说明就明白了。
myblind 2003-01-05
  • 打赏
  • 举报
回复
同意,实际它已经变成了汇编代码,但在输入前面已经输入了 '\n'
cjnet 2003-01-05
  • 打赏
  • 举报
回复
你可以先清除缓冲区的内容,然后再进行输入!
normalnotebook 2003-01-05
  • 打赏
  • 举报
回复
上面都说的不错
scanf("%s",stuff[i].sale);是遇到回车符结束,既接受的回车符前面的字符,回车符并未接受,则回车符还在缓冲区中
scanf("%c",&s);是%c则把缓冲区中的回车符接受了,故编译器不予理睬,直接跳过
anghna 2003-01-05
  • 打赏
  • 举报
回复
加个*号试试
chinajiji 2003-01-05
  • 打赏
  • 举报
回复
改正如下:

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

# include <conio.h>

int main()
{
struct classes{
char name[30];
double price;
double sale;
double income;
}stuff[100];

int select;
int i;
char s;

printf("**************************************\n");
printf("*1. data input 2. data account *\n");
printf("*3. data output 4. quit *\n");
printf("**************************************\n");

scanf("%d",&select);
switch(select)
{
case 1:
for(i=0;i<100;i++)
{
printf("\nPlease input the name: ");
scanf("%s",stuff[i].name);
fflush(stdin);
printf("\nPlease input the price: ");
scanf("%f",&stuff[i].price);
fflush(stdin);
printf("\nPlease input the sale:");
scanf("%f",&stuff[i].sale);
fflush(stdin);
stuff[i].income=stuff[i].price*stuff[i].sale;
printf("Would you want to creat another stuff?(y/n)");
scanf("%c",&s);
fflush(stdin);
if(s=='n')
break;
}
break;
}
system("PAUSE");
return 0;
}

SwordMan2001 2003-01-05
  • 打赏
  • 举报
回复
受缓冲区中的回车符影响。
在这一句前面加 flushall(); 或 getchar();试一试。

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧