繁体   English   中英

在 C 中退出 while 循环

[英]Exiting a while loop in C

#include <stdio.h>
#define true 1
#define false 0

int main(int argc, char *argv[])
{
    float celsius, fahrenheit;
    while(true) {

    printf("Please input a temperature in Celsius (type 'stop' to stop): " );
    scanf("%f", &celsius);

    fahrenheit = (1.8 * celsius) + 32;
    printf("Temperature in Fahrenheit: %f ", fahrenheit);
    }

    return(0);

}

我正在尝试用 C 编写一个将摄氏度转换为华氏度的程序。 我希望程序继续循环,直到用户输入“停止”。 退出到 C 中的 while 循环的最佳方法是什么?

由于stop不会转换为浮点数,因此您应该测试scanf()的返回值,因此:

#include <stdio.h>

int main(void)
{
    float celsius, fahrenheit;
    while (1)
   {
        printf("Please input a temperature in Celsius (type 'stop' to stop): ");
        if (scanf("%f", &celsius) != 1)
            break;

        fahrenheit = (1.8 * celsius) + 32;
        printf("Temperature in Fahrenheit: %f\n", fahrenheit);
    }

    return(0);
}

break语句实际上中断了循环。 请注意,如果您键入“ abracadabra”或非浮点值的任何其他内容,此操作将停止。 用换行符终止非提示输出。

创建while循环(如果您输入“ stop”会中断)的最简单方法是先将输入读取为字符串,然后检查输入是否等于“ stop”,否则将其转换为浮点变量。 我对您的代码做了一些更改。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BUFFER_SIZE     0x20

int main ( int argc, char **argv )
{
    char buffer [ BUFFER_SIZE ];
    char *exitword = "stop";
    float celsius, fahrenheit;

    while ( 0x01 )
    {
            memset ( buffer, 0x00, BUFFER_SIZE );

            printf("Please input a temperature in Celsius (type '%s' to stop): ", exitword );

            if ( fgets ( buffer, BUFFER_SIZE - 0x01, stdin ) == NULL )
            {
                    fprintf ( stderr, "Unable to read user input!\n" );
                    fprintf ( stderr, "Please try again\n" );
                    continue;
            }

            if ( strncmp ( buffer, exitword, strlen ( exitword )) == 0x00 )
            {
                    printf ( "Good bye!\n" );
                    break;
            }

            if (( celsius = strtof ( buffer, NULL )) == 0 )
            {
                    fprintf ( stderr, "Unable to convert user input to floating point number\n" );
                    fprintf ( stderr, "Please try again\n" );
                    continue;
            }

            fahrenheit = (1.8 * celsius) + 32;
            printf("Temperature in Fahrenheit: %f\n", fahrenheit);
    }

    return EXIT_SUCCESS;
}

该程序在某些约束下可以正常运行。 这对于整数两位数的celcius值非常有效 。将使用以“ stop”初始化的字符串,并将其与用户输入的字符串进行比较。 如果用户输入两位整数,则字符串将转换为数字,然后进行进一步的计算

#include <stdio.h>
#include <string.h>
#define true 1





int main(int argc, char *argv[])
{
    int celsius=0;
    float fahrenheit;
    char stop[]="stop";
    char checkForStop[10];
    int i;
    int array[10];

    while(true) 
    {

       printf("Please input a temperature in Celsius (type 'stop' to stop): " );
       gets(checkForStop);

       if(strcmp(stop,checkForStop)==0)
         {
           break;
         }
       else
         {
            for(i=0;i<2;i++)
              {
                array[i]=checkForStop[i]-'0';  // converts character to integer
              }
            celsius = array[0]*10+array[1];
         }  


        fahrenheit = (1.8 * celsius) + 32;`enter code here`
        printf("Temperature in Fahrenheit: %f ", fahrenheit);
    }

    return(0);

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM