Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当用户输入字母“while”时,如何退出while循环;q";-C程序设计_C_While Loop - Fatal编程技术网

当用户输入字母“while”时,如何退出while循环;q";-C程序设计

当用户输入字母“while”时,如何退出while循环;q";-C程序设计,c,while-loop,C,While Loop,正在完成这项任务。不知道如何摆脱while循环,我不能使用比while循环更高级的东西。我尝试将int转换为char,但没有成功。下面是我的代码。任何帮助都将不胜感激 /* This program has the user input a number. This number is then printed out in degrees F, C and K*/ #include <stdio.h> #define KEL 273.16 #define CEL1 32 #de

正在完成这项任务。不知道如何摆脱while循环,我不能使用比while循环更高级的东西。我尝试将int转换为char,但没有成功。下面是我的代码。任何帮助都将不胜感激

/* This program has the user input a number. This number is then printed out in degrees F, C and K*/

#include <stdio.h>

#define KEL 273.16
#define CEL1 32
#define CEL2 0.5555

void temperatures(double temp);


int main(void)
{
    double tempF; // Temperature the user inputs

    char userinput; // Quit character the user inputs

    userinput = 'a';

    tempF = 0;

    printf("Enter a temperature in degrees Farenheit or enter q to quit:\n");
    scanf_s("%lf", &tempF);
    //scanf_s("%c", &userinput);

    while ((char)tempF  != 'q')
    {
        temperatures(tempF);
        printf("Enter a temperature in degrees Farenheit or enter q to quit:\n");
        scanf_s("%lf", &tempF);
        //scanf_s("%c", &userinput);

    }

    printf("Goodbye!\n");


    return 0;
}

void temperatures(double temp)
{
    double celsius;
    double kelvins;

    celsius = (temp - CEL1) * CEL2;
    kelvins = celsius + KEL;

    printf("%lf F is %lf degrees C or %lf degrees K.\n", temp, celsius, kelvins);


}
/*此程序让用户输入一个数字。然后以F、C和K度打印出该数字*/
#包括
#定义KEL 273.16
#定义CEL1 32
#定义CEL2 0.5555
空隙温度(双温);
内部主(空)
{
double tempF;//用户输入的温度
char userinput;//退出用户输入的字符
userinput='a';
tempF=0;
printf(“以华氏度为单位输入温度,或输入q退出:\n”);
扫描频率(“%lf”、&tempF);
//scanf_s(“%c”、&userinput);
而((char)tempF!=“q”)
{
温度(tempF);
printf(“以华氏度为单位输入温度,或输入q退出:\n”);
扫描频率(“%lf”、&tempF);
//scanf_s(“%c”、&userinput);
}
printf(“再见!\n”);
返回0;
}
空隙温度(双温)
{
双摄氏度;
双开尔文;
摄氏=(温度-CEL1)*CEL2;
开尔文=摄氏度+开尔文;
printf(“%lf F为%lf摄氏度或%lf摄氏度。\n”,温度,摄氏度,开尔文);
}

您需要改变策略

  • 读一行文字
  • 如果行的第一个字母是
    q
    ,则退出
  • 否则,尝试使用
    sscanf
    从行中读取数字
  • 这里有一个版本的
    main
    可以做到这一点

    int main(void)
    {
       double tempF; // Temperature the user inputs
       char line[200];
    
       while ( 1 )
       {
          printf("Enter a temperature in degrees Fahrenheit or enter q to quit:\n");
    
          // Read a line of text.
          if (fgets(line, sizeof(line), stdin) == NULL )
          {
             break;
          }
    
          if ( line[0] == 'q' )
          {
             break;
          }
    
          if ( sscanf(line, "%lf", &tempF) == 1 )
          {
             // Got the temperature
             // Use it.
          }
          else
          {
             // The line does not have a number
             // Deal with the error.
          }
       }
    
       printf("Goodbye!\n");
    
       return 0;
    }
    

    你需要改变你的策略

  • 读一行文字
  • 如果行的第一个字母是
    q
    ,则退出
  • 否则,尝试使用
    sscanf
    从行中读取数字
  • 这里有一个版本的
    main
    可以做到这一点

    int main(void)
    {
       double tempF; // Temperature the user inputs
       char line[200];
    
       while ( 1 )
       {
          printf("Enter a temperature in degrees Fahrenheit or enter q to quit:\n");
    
          // Read a line of text.
          if (fgets(line, sizeof(line), stdin) == NULL )
          {
             break;
          }
    
          if ( line[0] == 'q' )
          {
             break;
          }
    
          if ( sscanf(line, "%lf", &tempF) == 1 )
          {
             // Got the temperature
             // Use it.
          }
          else
          {
             // The line does not have a number
             // Deal with the error.
          }
       }
    
       printf("Goodbye!\n");
    
       return 0;
    }
    

    您应该将输入读取为字符串()并将其转换为(),而不是使用exit命令来比较输入字符串。
    您还可以使用检查输入(不要尝试转换任何非数字的内容)

    您应该将输入读取为字符串()并将其转换(),而不是使用将输入字符串与exit命令进行比较。
    您还可以使用检查输入(不要尝试转换任何非数字的内容)

    您可以使用

    char c;
    while (c != 'q') {
        scanf("%c", &c);
        //parse command
    }
    

    你可以用汉字来读

    char c;
    while (c != 'q') {
        scanf("%c", &c);
        //parse command
    }