scanf for char输入被跳过 [英] scanf for char input getting skipped

查看:48
本文介绍了scanf for char输入被跳过的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!我是C的新手,我遇到了以下问题。在

以下程序中,最后一个scanf被忽略,程序

退出。谁能看到任何我做错的事情?谢谢

预付款。


#include< stdio.h>


/ *

家庭作业:作业1

姓名:Dennis McQuilken

分类:C编程

日期:9.19.2008

描述:这是一个C程序,将根据工时和小时费率计算员工

毛工资。

* /

int main()

{


int clock_number = 0;

float hours_worked = 0;

float hourly_rate = 0;

float total = 0;

char quitVal;


goto start;


开始:


printf("请输入员工的时钟号:\ n");

scanf("%i"& clock_number);

printf("请输入员工的小时费率:\ n");

scanf ("%f"& hourly_rate);

printf("请输入工作时数:\ n");

scanf("%f",& hours_worked);

total = hourly_rate * hours_worked;

printf(" Employee Clock Number \ tHourly Rate\tHours Worked\Gross\\\
");

printf("%i \\\\\\\\\\\\\\\\\\\\\\\\\ \t\%0.2f\ n",clock_number,

hours_worked,hourly_rate,total);

printf("你想要退出:" ;);

scanf("%c"& quitVal);

goto done;


done:


返回0;

}

解决方案

FerrisUML说:


大家好!我是C的新手,我遇到了以下问题。在

以下程序中,最后一个scanf被忽略



不,它不会被忽略。
< blockquote class =post_quotes>
和程序

退出。谁能看到任何我做错的事情?



是的。我已从您的程序中删除了与此特定问题无关的所有内容。


#include< stdio.h>


int main()

{

float hours_worked = 0;

char quitVal;


scanf("%f"& hours_worked);

printf(你想退出:);

scanf("%c"& quitVal);


返回0;

}



这个较短的程序会给你同样的问题 - 它似乎忽略了第二个scanf的
。但事实上它并没有这样做。


让我们说你的hours_worked值是4.2 - 所以你键入什么?它只是输入4.2就好了没有

因为光标会在你身边闪烁,在

的情况下你要添加一个5来使它成为4.25 ,是吗?


所以你按下ENTER键,告诉你的命令处理器你已经完成了这行数据。字符''4'',''。'',''2'',''\ n''现在是

,它们的标准输入流可用于您的C程序。 scanf

用'4'',''。''和''''来表示4.2,但''\\ n'''不是

浮点值,所以scanf将它留在流中。


你的下一个scanf从stdin读取一个字符。哦,看,有一个

''\ n''等待 - 这样做会很好,并且没有必要收集更多的输入

来自用户。


-

Richard Heathfield< http://www.cpax.org.uk>

电子邮件:-http:// www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日


9月17日,10:38 * pm,Richard Heathfield< r ... @ see.sig.invalidwrote:
< blockquote class =post_quotes>
FerrisUML说:


大家好! *我是C的新手,我遇到以下问题。 *在

下面的程序中,最后一个scanf被忽略



不,它不会被忽略。


和程序

退出。 *任何人都可以看到我做错的事吗?



是的。我已从程序中删除了与此特定问题无关的所有内容。


#include< stdio.h>


int main()

{

float hours_worked = 0;

char quitVal;


scanf("%f",& hours_worked);

printf("你想退出吗? :");

scanf("%c",& quitVal);


返回0;

}



这个更短程序会给你同样的问题 - 它似乎忽略了

第二个scanf。但事实上它并没有这样做。


让我们说你的hours_worked值是4.2 - 所以你键入什么?它只是输入4.2就好了没有

因为光标会在你身边闪烁,在

的情况下你要添加一个5来使它成为4.25 ,是吗?


所以你按下ENTER键,告诉你的命令处理器你已经完成了这行数据。字符''4'',''。'',''2'',''\ n''现在是

,它们的标准输入流可用于您的C程序。 scanf

用'4'',''。''和''''来表示4.2,但''\\ n'''不是

浮点值,所以scanf将它留在流中。


你的下一个scanf从stdin读取一个字符。哦,看,有一个

''\ n''等待 - 这样做会很好,并且没有必要收集更多的输入

来自用户。


-

Richard Heathfield< http://www.cpax.org.uk>

电子邮件:-http:// www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日



我有一种感觉,无论回应者会怎么说。什么是最好的方式

在执行scanf之前清除输入流缓冲区?我已经研究了
并找到了以下内容,但我不确定它是否是最好的

方法,因为我必须将scanf替换为getchar。

printf(你想为另一名员工计算吗?(是/否)\ n);

while(getchar()!=''\ n'')继续;

if(getchar()==''y'')

{

goto start;

}

其他

{

转到完成;

}


FerrisUML说:


< snip>


我有一种感觉,无论回应者会怎么说。什么是最好的方式

在执行scanf之前清除输入流缓冲区?



int fpclear(FILE * fp)

{

int ch;

while((ch = getc(fp))!=''\ n''&& ch!= EOF)

{

继续;

}

返回ch == EOF;

}


最简单的用法:


fpclear(stdin);

哦,如果你想大量使用comp.lang.c,我建议你学习

如何使用循环。如果你继续在其中发布带有转到的程序,那么足够的

clc订阅者将用barf来填充整个浴缸。


-

Richard Heathfield< http://www.cpax.org.uk>

电子邮件:-http:// www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日


Hello everyone! I new to C and am having the following problem. In
the below program, the last scanf is being ignored and the program
exits. Can anyone see anything that im doing wrong? Thanks in
advance.

#include <stdio.h>

/*
HOMEWORK: Assignment 1
Name: Dennis McQuilken
Class: C Programming
Date: 9.19.2008
Description: This is a C program that will calculate an employees
gross pay based on hours work and hourly rate.
*/
int main()
{

int clock_number = 0;
float hours_worked = 0;
float hourly_rate = 0;
float total = 0;
char quitVal;

goto start;

start:

printf("Please enter the employee''s clock number:\n");
scanf("%i", &clock_number);
printf("Please enter the employee''s hourly rate:\n");
scanf("%f", &hourly_rate);
printf("Please enter the hours worked:\n");
scanf("%f", &hours_worked);
total = hourly_rate * hours_worked;
printf("Employee Clock Number\tHourly Rate\tHours Worked\tGross\n");
printf("%i\t\t\t%0.2f\t\t%0.2f\t\t%0.2f\n", clock_number,
hours_worked, hourly_rate, total);
printf("Do you want to quit:");
scanf("%c", &quitVal);
goto done;

done:

return 0;
}

解决方案

FerrisUML said:

Hello everyone! I new to C and am having the following problem. In
the below program, the last scanf is being ignored

No, it isn''t being ignored.

and the program
exits. Can anyone see anything that im doing wrong?

Yes. I have removed from your program everything that is not relevant to
this specific problem.

#include <stdio.h>

int main()
{
float hours_worked = 0;
char quitVal;

scanf("%f", &hours_worked);
printf("Do you want to quit:");
scanf("%c", &quitVal);

return 0;
}

This shorter program will give you the same problem - it appears to ignore
the second scanf. But in fact it is not doing so.

Let''s say your hours_worked value is 4.2 - so what do you type? It''s no
good just typing 4.2 because the cursor will sit there blinking at you, in
case you''re about to add a 5 to make it 4.25, yes?

So you press ENTER to signify to your command processor that you''ve
finished this line of data. The characters ''4'', ''.'', ''2'', ''\n'' are now
made available to your C program on its standard input stream. The scanf
munches the ''4'', ''.'', and ''2'' to make 4.2, but ''\n'' isn''t part of a
floating point value, so scanf leaves it in the stream.

Your next scanf reads a single character from stdin. Oh look, there''s a
''\n'' waiting - that''ll do nicely, and there''s no need to gather more input
from the user.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


On Sep 17, 10:38*pm, Richard Heathfield <r...@see.sig.invalidwrote:

FerrisUML said:

Hello everyone! *I new to C and am having the following problem. *In
the below program, the last scanf is being ignored


No, it isn''t being ignored.

and the program
exits. *Can anyone see anything that im doing wrong?


Yes. I have removed from your program everything that is not relevant to
this specific problem.

#include <stdio.h>

int main()
{
float hours_worked = 0;
char quitVal;

scanf("%f", &hours_worked);
printf("Do you want to quit:");
scanf("%c", &quitVal);

return 0;
}


This shorter program will give you the same problem - it appears to ignore
the second scanf. But in fact it is not doing so.

Let''s say your hours_worked value is 4.2 - so what do you type? It''s no
good just typing 4.2 because the cursor will sit there blinking at you, in
case you''re about to add a 5 to make it 4.25, yes?

So you press ENTER to signify to your command processor that you''ve
finished this line of data. The characters ''4'', ''.'', ''2'', ''\n'' are now
made available to your C program on its standard input stream. The scanf
munches the ''4'', ''.'', and ''2'' to make 4.2, but ''\n'' isn''t part of a
floating point value, so scanf leaves it in the stream.

Your next scanf reads a single character from stdin. Oh look, there''s a
''\n'' waiting - that''ll do nicely, and there''s no need to gather more input
from the user.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999

I had a feeling whomever responded would say that. Whats the best way
to clear the input stream buffer before peforming a scanf? I''ve
researched and found the following, but im not sure if its the best
approach seeing as I have to swap scanf for getchar.
printf("Would you like to calculate for another employee? (y/n)\n");
while(getchar() != ''\n'') continue;
if(getchar() == ''y'')
{
goto start;
}
else
{
goto done;
}


FerrisUML said:

<snip>

I had a feeling whomever responded would say that. Whats the best way
to clear the input stream buffer before peforming a scanf?

int fpclear(FILE *fp)
{
int ch;
while((ch = getc(fp)) != ''\n'' && ch != EOF)
{
continue;
}
return ch == EOF;
}

Simplest possible usage:

fpclear(stdin);
Oh, and if you want to make much use of comp.lang.c, I suggest you learn
how to use loops. If you keep posting programs with goto in them, enough
clc subscribers will barf to fill an entire tub.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


这篇关于scanf for char输入被跳过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆