按'q'或非数字字符来退出循环/程序 [英] pressing 'q' or non-numeric char to exit loop/program
问题描述
这是一个简单的温度转换器(华氏温度到摄氏温度,开元价格为b
)。使用gcc 4.0。
用户应该输入q或任何其他非字符来退出
程序。
此代码出现问题:
一旦程序启动,我输入''q''。它挂了。
然后再次运行它。输入一个数字,它执行正常。但是当我输入
q时,它会重复上一个数字并且它没有注意输入的字母
。
关于如何修复它的任何想法?提前谢谢。
#include< stdio.h>
const double F_TO_CELS_ONE_EIGHT = 1.8;
const double F_TO_CELS_THIRTYTWO = 32.0;
const double C_TO_KELVIN = 273.16;
void Temperatures(double f_temp);
int main(void){
double f_temp;
char quit;
while(quit!=" q" ){
printf(输入华氏温度(q退出):
");
quit = getchar();
scanf("%lf",& f_temp);
Temperatures(f_temp);
}
printf(" \ nbyby!");
返回0;
}
void Temperatures(double f_temp ){
双摄氏度,开尔文;
摄氏度=(F_TO_CELS_ONE_EIGHT * f_temp)+ F_TO_CELS_THIRTYTWO;
开尔文=摄氏+ C_TO_KELVIN;
printf(华氏度:%。2f \ n,f_temp);
printf(摄氏度:%。2f \ n,摄氏度);
printf(开尔文度:%。2f \ n,开尔文);
}
icarus说:
< snip>
关于如何修复它的任何想法?
是。
#include< stdio.h>
const double F_TO_CELS_ONE_EIGHT = 1.8;
const double F_TO_CELS_THIRTYTWO = 32.0;
const double C_TO_KELVIN = 273.16;
void Temperatures(double f_temp);
int main(void){
double f_temp;
char quit ;
int quit; / * getchar返回int,而不是char * /
>
while(quit!=" q") {
while(quit!=''q''){
" q"是一个字符串文字。你想要与一个角色进行比较,而不是一个
字符串。
printf("以华氏温度输入温度(q退出):
");
quit = getchar();
scanf("%lf",& f_temp);
坏主意。假设您输入字符2,1,2(沸点
水)和换行符。第一个2存储在退出中,
将1和2转换为12,存储在f_temp中。
现在,12华氏度比沸点冷得多(事实上,它比冰冻低20美元)。
我建议捕获数据作为一个字符串并检查第一个
字符。如果它不是''q'',请将字符串交给sscanf(或者,更好的是
仍然是strtod)进行转换。
- -
Richard Heathfield< http://www.cpax.org.uk>
电子邮件:-http:// www。 + rjh @
谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>
Usenet是一个奇怪的放置" - dmr 1999年7月29日
icarus写道,On 09/05/08 21:57:
Hi ,这是一个简单的温度转换器(华氏温度到摄氏温度),开元温度为b
。使用gcc 4.0。
用户应该输入q或任何其他非字符来退出
程序。
此代码出现问题:
一旦程序启动,我输入''q''。它挂了。
然后再次运行它。输入一个数字,它执行正常。但是当我输入
q时,它会重复上一个数字并且它没有注意输入的字母
。
关于如何修复它的任何想法?提前谢谢。
#include< stdio.h>
const double F_TO_CELS_ONE_EIGHT = 1.8;
const double F_TO_CELS_THIRTYTWO = 32.0;
const double C_TO_KELVIN = 273.16;
void Temperatures(double f_temp);
int main(void){
double f_temp;
char quit;
这里有什么价值,因为它从未被赋值?
while(quit) !=" q"){
printf("输入华氏温度(q退出):
");
quit = getchar();
所以你已经阅读了一个字符,现在你去调用scanf无论是什么
suer输入!您在该行末尾输入的换行符会发生什么变化?
scanf("%lf",& f_temp);
< snip>
scanf返回一个值,这是什么意思?
你需要首先考虑用户输入是如何工作的,看起来好像
你在写这篇文章之前没有想到它。我建议
使用fgets输入然后检查并使用输入的内容。
-
Flash Gordon
你需要首先考虑用户输入是如何工作的,它看起来像
你在写这篇文章之前没有想到它。我建议
使用fgets输入然后检查并使用输入的内容。
-
Flash Gordon
是啊是啊是的闪光戈登,给我讲课。
如果你这么体贴,
为什么要穿那么你对可能的解决方案进行详细说明了吗?
5月9日上午11点48分,Flash Gordon< s ... @ flash- gordon.me.ukwrote:
icarus写道,On 09/05/08 21:57:
这是一个简单的温度转换器(华氏温度到摄氏温度,开元价格为b
)。使用gcc 4.0。
用户应该输入q或任何其他非字符来退出
程序。
此代码出现问题:
一旦程序启动,我输入''q''。它挂了。
然后再次运行它。输入一个数字,它执行正常。但是当我输入
q时,它会重复上一个数字并且它不会注意输入的字母
。
关于如何修复它的任何想法?提前致谢。
#include< stdio.h>
const double F_TO_CELS_ONE_EIGHT = 1.8;
const double F_TO_CELS_THIRTYTWO = 32.0;
const double C_TO_KELVIN = 273.16;
void Temperatures(double f_temp);
int main(void){
double f_temp;
char quit;
因为它从未被赋值,所以退出这里有什么值?
while(quit !=" q"){
printf("输入华氏温度(q退出):
");
quit = getchar();
所以你已经阅读了一个角色,现在你去打电话给scanf无论是什么
suer输入!您在该行末尾输入的换行符会发生什么变化?
scanf("%lf",& f_temp);
< snip>
scanf返回一个值,这是什么意思?
你需要首先考虑用户输入是如何工作的,看起来好像
你在写这篇文章之前没有想到它。我建议
使用fgets输入然后检查并使用输入的内容。
-
Flash Gordon
Hi, this is a simple temperature converter (Fahrenheit to Celsius to
Kelvin). Using gcc 4.0.
The user is supposed to enter q or any other non-character to exit the
program.
Problem with this code:
I enter ''q'' as soon as the program starts. It hangs.
Then run it again. Enter a number, it executes fine. But when I enter
q, it repeats last number and it doesn''t ''pay attention'' to the letter
entered.
Any ideas on how can I fix it? thanks in advance.
#include <stdio.h>
const double F_TO_CELS_ONE_EIGHT = 1.8;
const double F_TO_CELS_THIRTYTWO = 32.0;
const double C_TO_KELVIN = 273.16;
void Temperatures(double f_temp);
int main(void){
double f_temp;
char quit;
while (quit != "q"){
printf("Enter temperature in Fahrenheit (q to quit):
");
quit = getchar();
scanf("%lf", &f_temp);
Temperatures(f_temp);
}
printf("\nbye!");
return 0;
}
void Temperatures(double f_temp){
double celsius, kelvin;
celsius = (F_TO_CELS_ONE_EIGHT * f_temp) + F_TO_CELS_THIRTYTWO;
kelvin = celsius + C_TO_KELVIN;
printf("Fahrenheit degrees: %.2f\n", f_temp);
printf("Celsius degrees: %.2f\n", celsius);
printf("Kelvin degrees: %.2f\n", kelvin);
}
icarus said:
<snip>
Any ideas on how can I fix it?Yes.
#include <stdio.h>
const double F_TO_CELS_ONE_EIGHT = 1.8;
const double F_TO_CELS_THIRTYTWO = 32.0;
const double C_TO_KELVIN = 273.16;
void Temperatures(double f_temp);
int main(void){
double f_temp;
char quit;int quit; /* getchar returns int, not char */
>
while (quit != "q"){while(quit != ''q''){
"q" is a string literal. You want to compare with a character, not a
string.
printf("Enter temperature in Fahrenheit (q to quit):
");
quit = getchar();
scanf("%lf", &f_temp);Bad idea. Let''s say you enter the characters ''2'', ''1'', ''2'' (boiling point
of water) and a newline character. The first ''2'' is stored in quit,
leaving ''1'' and ''2'' to be converted into 12 which is stored in f_temp.
Now, 12 Fahrenheit is a lot colder than boiling point (in fact, it''s 20
below freezing).
I recommend capturing the data as a string and inspecting the first
character. If it''s not ''q'', hand the string off to sscanf (or, better
still, strtod) for conversion.
--
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
icarus wrote, On 09/05/08 21:57:Hi, this is a simple temperature converter (Fahrenheit to Celsius to
Kelvin). Using gcc 4.0.
The user is supposed to enter q or any other non-character to exit the
program.
Problem with this code:
I enter ''q'' as soon as the program starts. It hangs.
Then run it again. Enter a number, it executes fine. But when I enter
q, it repeats last number and it doesn''t ''pay attention'' to the letter
entered.
Any ideas on how can I fix it? thanks in advance.
#include <stdio.h>
const double F_TO_CELS_ONE_EIGHT = 1.8;
const double F_TO_CELS_THIRTYTWO = 32.0;
const double C_TO_KELVIN = 273.16;
void Temperatures(double f_temp);
int main(void){
double f_temp;
char quit;What value does quit have here since it has never been assigned a value?
while (quit != "q"){
printf("Enter temperature in Fahrenheit (q to quit):
");
quit = getchar();So you have read one character, now you go and call scanf whatever the
suer input! What happens to the newline you enter at the end of the line?
scanf("%lf", &f_temp);<snip>
scanf returns a value, what does it mean?
You need to start by thinking about how user input works, it looks like
you did not think about it before writing this. I would suggest that
using fgets for input and then checking and using what was entered.
--
Flash Gordon
You need to start by thinking about how user input works, it looks likeyou did not think about it before writing this. I would suggest that
using fgets for input and then checking and using what was entered.
--
Flash Gordonyeah yeah yeah flash gordon, spare me the lecture.
if you are so thoughtful,
why don''t you elaborate a little more on your possible solution then?
On May 9, 11:48 am, Flash Gordon <s...@flash-gordon.me.ukwrote:icarus wrote, On 09/05/08 21:57:
Hi, this is a simple temperature converter (Fahrenheit to Celsius to
Kelvin). Using gcc 4.0.
The user is supposed to enter q or any other non-character to exit the
program.
Problem with this code:
I enter ''q'' as soon as the program starts. It hangs.
Then run it again. Enter a number, it executes fine. But when I enter
q, it repeats last number and it doesn''t ''pay attention'' to the letter
entered.
Any ideas on how can I fix it? thanks in advance.
#include <stdio.h>
const double F_TO_CELS_ONE_EIGHT = 1.8;
const double F_TO_CELS_THIRTYTWO = 32.0;
const double C_TO_KELVIN = 273.16;
void Temperatures(double f_temp);
int main(void){
double f_temp;
char quit;
What value does quit have here since it has never been assigned a value?
while (quit != "q"){
printf("Enter temperature in Fahrenheit (q to quit):
");
quit = getchar();
So you have read one character, now you go and call scanf whatever the
suer input! What happens to the newline you enter at the end of the line?
scanf("%lf", &f_temp);
<snip>
scanf returns a value, what does it mean?
You need to start by thinking about how user input works, it looks like
you did not think about it before writing this. I would suggest that
using fgets for input and then checking and using what was entered.
--
Flash Gordon
这篇关于按'q'或非数字字符来退出循环/程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!