0

我有一个作业,我应该以以下形式接受用户的输入:

double, char('C' for Celsius or 'F' for Fahrenheit)

并将其转换为其他温度标度。为了完成这个任务,我编写了这个程序:

该程序

/*
  The purpose of these program is to convert a user's input and use it in a tempurate conversion programs.
  By Paramjot Singh

  Psuedocode:

  import all needed files

  ask for user's input.

  convert user's imput into 2 varibles: a double and a char

  if the char is 'c' convert the double to farinhiet
  if the char is 'c' convert the double to celius

  display the result.
*/

 #include <stdio.h>
 int main( )
 {
    char input[7];
    printf("Welcome to the Tempurate Conversion Enter a number followed by C or F, depending on if you what to convert a Celuis Temp to Farinheit or vice versa.");
    fgets(input, 7, stdin);
    /*if (in == 'C' || in == 'c') commnented out code
    {
        int f = 9 / 5 (inint + 32);
        printf("Input ", in, " Output: " f, " F");
    }
     else (inint == 'F' || inint == 'f')
    {
        int c = 5 / 9 (inint - 32);
        printf("Input ", in, " Output: " c, " C");
    }
     else 
    {
       printf("I told you to enter c or f. Restart the program.");
    } */

     /*to test what was entered*/
     printf(input);
     return 0;
}

我的问题是如何将部分字符数组转换为双精度。

4

2 回答 2

1

根据 printf 语句:

printf("Welcome to the Tempurate Conversion Enter a number followed by C or F, depending on if you what to convert a Celuis Temp to Farinheit or vice versa.");

看起来您希望用户输入(例如):23.5c

这意味着:scanf("%lf%c", &inDouble, &inChar);没有中间逗号的语句。` 请注意对变量名称的更改。

此外,这tolower(inChar)将简化您的一些 if 逻辑。

希望这可以帮助。

于 2013-08-09T23:12:22.720 回答
0

使用以下命令扫描您的输入sscanf

sscanf(input, "%lf, %c", &inint, &in);

编辑: inint是一个有点误导性的名字。你应该双读,对吧?

于 2013-08-09T17:42:36.733 回答