【发布时间】:2020-09-04 21:15:20
【问题描述】:
所以我正在尝试做一个从摄氏到华氏的温度转换器,由于某种原因,我的代码的输出全部被破坏了。这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <readline/readline.h>
#include <string.h>
int main(void)
{
char *temperature = readline("Enter a temperature in celsius: ");
double t1 = ((double)*temperature);
double t2 = ((double)*temperature * 1.8) + 32;
printf("%f° in Celsius is equivalent to %f° Fahrenheit.", t1, t2);
return 0;
}
输出:
Enter a temperature in celsius: 100
49.000000° in Celsius is equivalent to 120.200000° Fahrenheit.
谁能告诉我我的代码有什么问题?
【问题讨论】:
-
您正试图通过类型转换将字符串转换为双精度。那是行不通的。考虑使用
sscanf之类的strtod将字符串转换为双精度。
标签: c readline temperature