急急急!小问题,帮大忙!
下面程序中倒数第七行的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;
}
}
问题点数:20、回复次数:10Top
1 楼SwordMan2001(天笑2001)回复于 2003-01-05 02:05:46 得分 10
受缓冲区中的回车符影响。
在这一句前面加 flushall(); 或 getchar();试一试。Top
2 楼chinajiji(菜鸟叽叽)回复于 2003-01-05 02:21:02 得分 5
改正如下:
#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;
}
Top
3 楼aurora_song(兔兔)回复于 2003-01-05 09:55:28 得分 5
可以肯定的是你在倒数第七行的scanf之前还有过其他scanf。SwordMan2001(天笑2001) 说的是对的,看看flushall()或者fflush(stdin)的用法和说明就明白了。Top
4 楼hongfeeling(无烟亦如烟)回复于 2003-01-05 10:19:28 得分 0
我也遇到过这样的情况。
把那个scanf("%c",&s);
改成:getchar()就可以了。
一直不知其所以然。看了楼上的说法有点明白了。
感谢!Top
5 楼chinajiji(菜鸟叽叽)回复于 2003-01-05 12:26:19 得分 0
flushall()好象不是C标准库函数,它是TC下的专用函数,而fflush()是C标准函数.用标准函数的程序可移植性要好些.Top
6 楼goodboy1881(积木)(谁都别拦着我在水源升星)回复于 2003-01-05 12:55:06 得分 0
是啊,是啊,我也是,那个scanf让我郁闷了好长的时间呢!
长经验值了Top
7 楼anghna(拓瞳)回复于 2003-01-05 15:39:22 得分 0
加个*号试试Top
8 楼normalnotebook(逐浪踏雪)回复于 2003-01-05 16:23:20 得分 0
上面都说的不错
scanf("%s",stuff[i].sale);是遇到回车符结束,既接受的回车符前面的字符,回车符并未接受,则回车符还在缓冲区中
scanf("%c",&s);是%c则把缓冲区中的回车符接受了,故编译器不予理睬,直接跳过
Top
9 楼cjnet(孤星剑)回复于 2003-01-05 16:59:28 得分 0
你可以先清除缓冲区的内容,然后再进行输入!Top
10 楼myblind(敲键盘的农民)回复于 2003-01-05 17:20:48 得分 0
同意,实际它已经变成了汇编代码,但在输入前面已经输入了 '\n'Top
相关问题
- 快来帮忙,急急急急急急急急急急急急
- 请大家帮忙!!急急急急急急急急急急急急急急!!!!!!!!!!!!!!!!!!!!!!!!!!!
- 急!急!急!急!急!急!高手帮忙!!!!!!!
- 急!急!急!急!急!急!高手帮忙!!!!!!!
- 请大家进来帮忙----------------------急急急急急急急急急急急急急急急!
- 帮帮我,急急急急!!!!
- 急急急急急急急急!!帮帮我
- 对层的控制!(高手帮忙呀!!!)(急急急急急急 急急急急 急急急急 )
- 急!急!急!急!急!急!急!急!急!急!急!急!急需 英文版 的 VS.net2003 !!!各位大狭帮帮忙!
- 急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急




