#include <stdio.h>
const double C_F1 = 1.8;
const double C_F2 = 32.0;
const double K_C = 273.16;
void temperature(double fah)
{
double cel, kel;
cel = C_F1 * fah + C_F2;
kel = cel + K_C;
printf ("Fahrenheit Celsius Kelvin\n");
printf ("%10.2Lf%9.2Lf%8.2Lf\n", fah, cel, kel);
}
int main(void)
{
double fahtem, celtem, keltem;
printf ("This program ask user input a temperature of Fahrenheit\n");
printf ("The program count the other two kinds of temperature\n");
printf ("Then display them\n");
printf ("You can input reapeatedly until input q\n");
//printf ("q or other non-numeric value\n");
printf ("Now input the value\n");
scanf ("%f", &fahtem);
while ((char)fahtem != 'q')
{
temperature(fahtem);
scanf ("%f", &fahtem);
//getchar();
}
printf ("The end!!!\n");
getchar();
getchar();
return 0;
}
程序的目的是转换摄氏,开氏,华氏温标,输入华氏温度,转换成摄氏和开氏并输出,输入q时退出循环,可是我用Dev C++编译后不能结束循环,帮忙看看是怎么回事啊,谢谢
21 个解决方案
#1
也许在循环判断while ((char)fahtem != 'q')中有错,即使你做了强制类型转换,但double 和 float 之类的"精度"数据不能和char 转换,因为double 和 char 的在CPU中是采用不同的编码.
#2
楼主,为什么我照拷你的东西到我的机子上面运行,无论输入什么,结果都是一样的呢?
dev C++、vc我都试过了
dev C++、vc我都试过了
#3
你不妨输入'q'然后显示出来,看一下在float格式下输出是多少,然后再把
while ((char)fahtem != 'q')
中的'q'替换成你输出的值,看看是不是可以.
while ((char)fahtem != 'q')
中的'q'替换成你输出的值,看看是不是可以.
#4
你的变量fahtem时double型的,即使你用char进行强制类型转换,但由于之前你输入q时已经通过scanf转换过一次了,q已经被改掉了,所以你得不到q的值,当然循环就不可能结束了,你应该改变一下输入的方法,使用一个字符串,将输入的温度用字符的形式保存,等输入结束后在用atof函数将字符串转为浮点数就应该可以了,那么你循环的判断条件只要用fahtem[0] != 'q'就行了
#5
scanf ("%f", &fahtem);接受的是double型,即使(char)fahtem也不行了。
你试试这样(char)((int)fahtem)或许可以。
楼上说的才是好的编程方法。
你试试这样(char)((int)fahtem)或许可以。
楼上说的才是好的编程方法。
#6
谢谢大家^_^to ekschencn(X) 你说的对,可是我看了我的程序,好像不应该这样啊,就程序来说
#7
我用vc跟踪了一下,发现当输入q的时候,fahtem并没有改变,仍然是上次输入的值,而且会忽略以后所有的scanf。我猜想在devc++了或许也是由于这个原因。(另外,我不得不把fahtem改成float才能成功地使用scanf。不知道有谁知道为什么呢?)
#8
可能这也是double类型的原因
#9
类型转换的问题
scanf ("%c", &fahtem);
while ((char)fahtem != 'q')
{
temperature(fahtem);
scanf ("%c", &fahtem);
//getchar();
}
scanf ("%c", &fahtem);
while ((char)fahtem != 'q')
{
temperature(fahtem);
scanf ("%c", &fahtem);
//getchar();
}
#10
如果输入的是温度值,那么楼上的应用的方法将会试取精度的,具体来说应该是不可以读入float类型。
#11
//test5_7.c--count the temperature of 3 kinds
#include <stdio.h>
const double C_F1 = 1.8;
const double C_F2 = 32.0;
const double K_C = 273.16;
void temperature(double fah)
{
double cel, kel;
cel = C_F1 * fah + C_F2;
kel = cel + K_C;
printf ("Fahrenheit Celsius Kelvin\n");
printf ("%10.2Lf%10.2Lf%9.2Lf\n", fah, cel, kel);
}
int main(void)
{
double fahtem;
printf ("This program ask user input a temperature of Fahrenheit\n");
printf ("The program count the other two kinds of temperature\n");
printf ("Then display them\n");
printf ("You can input reapeatedly until input q\n");
//printf ("q or other non-numeric value\n");
printf ("Now input the value\n");
//scanf ("%f", &fahtem);
while (scanf("%Lf", &fahtem) == 1) //利用scanf的返回值看是否成功读入
{
temperature(fahtem);
//scanf ("%f", &fahtem);
//getchar();
}
printf ("The end!!!\n");
getchar();
getchar();
getchar();
return 0;
}
#include <stdio.h>
const double C_F1 = 1.8;
const double C_F2 = 32.0;
const double K_C = 273.16;
void temperature(double fah)
{
double cel, kel;
cel = C_F1 * fah + C_F2;
kel = cel + K_C;
printf ("Fahrenheit Celsius Kelvin\n");
printf ("%10.2Lf%10.2Lf%9.2Lf\n", fah, cel, kel);
}
int main(void)
{
double fahtem;
printf ("This program ask user input a temperature of Fahrenheit\n");
printf ("The program count the other two kinds of temperature\n");
printf ("Then display them\n");
printf ("You can input reapeatedly until input q\n");
//printf ("q or other non-numeric value\n");
printf ("Now input the value\n");
//scanf ("%f", &fahtem);
while (scanf("%Lf", &fahtem) == 1) //利用scanf的返回值看是否成功读入
{
temperature(fahtem);
//scanf ("%f", &fahtem);
//getchar();
}
printf ("The end!!!\n");
getchar();
getchar();
getchar();
return 0;
}
#12
程序已经修改好,现在知道了在输入q后无限循环的原因是因为scanf函数要求是读入double类型,在键入q时,scanf函数读入不成功,但读入的指针仍然指向q,下一次读入从这个指针开始读取,仍然读入不成功,从而造成循环.
另外,对于前面说的输入数字后结果都是一样的原因,还不是很清楚,这次我把输入时的控制字符串改为%Lf(本来就是要求读入double类型,原来搞错了),就可以了,但是具体原因还得向大家请教一下
另外,对于前面说的输入数字后结果都是一样的原因,还不是很清楚,这次我把输入时的控制字符串改为%Lf(本来就是要求读入double类型,原来搞错了),就可以了,但是具体原因还得向大家请教一下
#13
回答的人真多,我就算了
哈哈
哈哈
#14
呵呵!
都是高手
都是高手
#15
说穿了,还不就是数据类型不匹配,这个搞得人最头大了。
#16
搞这么复杂干嘛,用一个变量来字符变量控制循环不就得了!
#17
scanf()回给据format自己过滤的,如果scanf()出处,要将输入缓冲flush一下(_flushall())
变通一下,因为温度不可能-1000度(不可能低于绝对0度以下),而且鉴于float/double的特性,不要做等值判断,只做<或>比较。
变通一下,因为温度不可能-1000度(不可能低于绝对0度以下),而且鉴于float/double的特性,不要做等值判断,只做<或>比较。
#18
错字连篇:(
==============================
scanf()会根据format自己过滤的,如果scanf()出错,要将输入缓冲flush一下(_flushall())
变通一下,因为温度不可能-1000度(不可能低于绝对0度以下),而且鉴于float/double的特性,不要做等值判断,只做<或>比较。
while (fahtem > -1000.0)
==============================
scanf()会根据format自己过滤的,如果scanf()出错,要将输入缓冲flush一下(_flushall())
变通一下,因为温度不可能-1000度(不可能低于绝对0度以下),而且鉴于float/double的特性,不要做等值判断,只做<或>比较。
while (fahtem > -1000.0)
#19
char flag=0;
while (1)
{
scanf("%f",&fahtm);
fflush(stdin);
temperature(fahtem);
printf("还要继续么?\n");
scanf("%c",&flag);
fflush(stdin);
if(flag=='q') break;
}
这样界面友好的多呵
while (1)
{
scanf("%f",&fahtm);
fflush(stdin);
temperature(fahtem);
printf("还要继续么?\n");
scanf("%c",&flag);
fflush(stdin);
if(flag=='q') break;
}
这样界面友好的多呵
#20
呵呵~楼主的问题我也遇到过。把fahtem改成float就可以了,double型的变量好象不能通过scanf来得到,只能通过赋值。具体原因我也不是很清楚。
#21
double 类型的不是可以通过 scanf("%Lf", &a)来得到吗?
大家说的一些东西我都还没有学到,呵呵,权当是知识储备吧,我后来改的程序是看了c primer plus改的
大家说的一些东西我都还没有学到,呵呵,权当是知识储备吧,我后来改的程序是看了c primer plus改的
#1
也许在循环判断while ((char)fahtem != 'q')中有错,即使你做了强制类型转换,但double 和 float 之类的"精度"数据不能和char 转换,因为double 和 char 的在CPU中是采用不同的编码.
#2
楼主,为什么我照拷你的东西到我的机子上面运行,无论输入什么,结果都是一样的呢?
dev C++、vc我都试过了
dev C++、vc我都试过了
#3
你不妨输入'q'然后显示出来,看一下在float格式下输出是多少,然后再把
while ((char)fahtem != 'q')
中的'q'替换成你输出的值,看看是不是可以.
while ((char)fahtem != 'q')
中的'q'替换成你输出的值,看看是不是可以.
#4
你的变量fahtem时double型的,即使你用char进行强制类型转换,但由于之前你输入q时已经通过scanf转换过一次了,q已经被改掉了,所以你得不到q的值,当然循环就不可能结束了,你应该改变一下输入的方法,使用一个字符串,将输入的温度用字符的形式保存,等输入结束后在用atof函数将字符串转为浮点数就应该可以了,那么你循环的判断条件只要用fahtem[0] != 'q'就行了
#5
scanf ("%f", &fahtem);接受的是double型,即使(char)fahtem也不行了。
你试试这样(char)((int)fahtem)或许可以。
楼上说的才是好的编程方法。
你试试这样(char)((int)fahtem)或许可以。
楼上说的才是好的编程方法。
#6
谢谢大家^_^to ekschencn(X) 你说的对,可是我看了我的程序,好像不应该这样啊,就程序来说
#7
我用vc跟踪了一下,发现当输入q的时候,fahtem并没有改变,仍然是上次输入的值,而且会忽略以后所有的scanf。我猜想在devc++了或许也是由于这个原因。(另外,我不得不把fahtem改成float才能成功地使用scanf。不知道有谁知道为什么呢?)
#8
可能这也是double类型的原因
#9
类型转换的问题
scanf ("%c", &fahtem);
while ((char)fahtem != 'q')
{
temperature(fahtem);
scanf ("%c", &fahtem);
//getchar();
}
scanf ("%c", &fahtem);
while ((char)fahtem != 'q')
{
temperature(fahtem);
scanf ("%c", &fahtem);
//getchar();
}
#10
如果输入的是温度值,那么楼上的应用的方法将会试取精度的,具体来说应该是不可以读入float类型。
#11
//test5_7.c--count the temperature of 3 kinds
#include <stdio.h>
const double C_F1 = 1.8;
const double C_F2 = 32.0;
const double K_C = 273.16;
void temperature(double fah)
{
double cel, kel;
cel = C_F1 * fah + C_F2;
kel = cel + K_C;
printf ("Fahrenheit Celsius Kelvin\n");
printf ("%10.2Lf%10.2Lf%9.2Lf\n", fah, cel, kel);
}
int main(void)
{
double fahtem;
printf ("This program ask user input a temperature of Fahrenheit\n");
printf ("The program count the other two kinds of temperature\n");
printf ("Then display them\n");
printf ("You can input reapeatedly until input q\n");
//printf ("q or other non-numeric value\n");
printf ("Now input the value\n");
//scanf ("%f", &fahtem);
while (scanf("%Lf", &fahtem) == 1) //利用scanf的返回值看是否成功读入
{
temperature(fahtem);
//scanf ("%f", &fahtem);
//getchar();
}
printf ("The end!!!\n");
getchar();
getchar();
getchar();
return 0;
}
#include <stdio.h>
const double C_F1 = 1.8;
const double C_F2 = 32.0;
const double K_C = 273.16;
void temperature(double fah)
{
double cel, kel;
cel = C_F1 * fah + C_F2;
kel = cel + K_C;
printf ("Fahrenheit Celsius Kelvin\n");
printf ("%10.2Lf%10.2Lf%9.2Lf\n", fah, cel, kel);
}
int main(void)
{
double fahtem;
printf ("This program ask user input a temperature of Fahrenheit\n");
printf ("The program count the other two kinds of temperature\n");
printf ("Then display them\n");
printf ("You can input reapeatedly until input q\n");
//printf ("q or other non-numeric value\n");
printf ("Now input the value\n");
//scanf ("%f", &fahtem);
while (scanf("%Lf", &fahtem) == 1) //利用scanf的返回值看是否成功读入
{
temperature(fahtem);
//scanf ("%f", &fahtem);
//getchar();
}
printf ("The end!!!\n");
getchar();
getchar();
getchar();
return 0;
}
#12
程序已经修改好,现在知道了在输入q后无限循环的原因是因为scanf函数要求是读入double类型,在键入q时,scanf函数读入不成功,但读入的指针仍然指向q,下一次读入从这个指针开始读取,仍然读入不成功,从而造成循环.
另外,对于前面说的输入数字后结果都是一样的原因,还不是很清楚,这次我把输入时的控制字符串改为%Lf(本来就是要求读入double类型,原来搞错了),就可以了,但是具体原因还得向大家请教一下
另外,对于前面说的输入数字后结果都是一样的原因,还不是很清楚,这次我把输入时的控制字符串改为%Lf(本来就是要求读入double类型,原来搞错了),就可以了,但是具体原因还得向大家请教一下
#13
回答的人真多,我就算了
哈哈
哈哈
#14
呵呵!
都是高手
都是高手
#15
说穿了,还不就是数据类型不匹配,这个搞得人最头大了。
#16
搞这么复杂干嘛,用一个变量来字符变量控制循环不就得了!
#17
scanf()回给据format自己过滤的,如果scanf()出处,要将输入缓冲flush一下(_flushall())
变通一下,因为温度不可能-1000度(不可能低于绝对0度以下),而且鉴于float/double的特性,不要做等值判断,只做<或>比较。
变通一下,因为温度不可能-1000度(不可能低于绝对0度以下),而且鉴于float/double的特性,不要做等值判断,只做<或>比较。
#18
错字连篇:(
==============================
scanf()会根据format自己过滤的,如果scanf()出错,要将输入缓冲flush一下(_flushall())
变通一下,因为温度不可能-1000度(不可能低于绝对0度以下),而且鉴于float/double的特性,不要做等值判断,只做<或>比较。
while (fahtem > -1000.0)
==============================
scanf()会根据format自己过滤的,如果scanf()出错,要将输入缓冲flush一下(_flushall())
变通一下,因为温度不可能-1000度(不可能低于绝对0度以下),而且鉴于float/double的特性,不要做等值判断,只做<或>比较。
while (fahtem > -1000.0)
#19
char flag=0;
while (1)
{
scanf("%f",&fahtm);
fflush(stdin);
temperature(fahtem);
printf("还要继续么?\n");
scanf("%c",&flag);
fflush(stdin);
if(flag=='q') break;
}
这样界面友好的多呵
while (1)
{
scanf("%f",&fahtm);
fflush(stdin);
temperature(fahtem);
printf("还要继续么?\n");
scanf("%c",&flag);
fflush(stdin);
if(flag=='q') break;
}
这样界面友好的多呵
#20
呵呵~楼主的问题我也遇到过。把fahtem改成float就可以了,double型的变量好象不能通过scanf来得到,只能通过赋值。具体原因我也不是很清楚。
#21
double 类型的不是可以通过 scanf("%Lf", &a)来得到吗?
大家说的一些东西我都还没有学到,呵呵,权当是知识储备吧,我后来改的程序是看了c primer plus改的
大家说的一些东西我都还没有学到,呵呵,权当是知识储备吧,我后来改的程序是看了c primer plus改的