c - 我不明白为什么我的 C 程序运行一次正确,然后却给出了不需要的结果

标签 c

我不明白为什么我的程序运行正确,然后当 do...while 重复时,当我不希望它进入我的 else 函数时。完成此操作后,我的程序就会像我想要的那样重复。我对这个糟糕的解释感到抱歉,但我认为“我的程序”下面的图片比我能解释正在发生的事情更好。

Execution image

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

int main()
{
float fahrenheit,celsius;
char selection[30];
char *idontknow;
long trueselection;

do{
printf("1: Fahrenheit to Celsius."); 
printf("\n2: Celsius to Fahrenheit.");
printf("\n3: Quit program.\n");
fgets(selection,10,stdin);  //This line and the line below are in the program 
//so that if the user inputs a character instead of an integer it does not infinite loop. 
trueselection = strtol(selection, &idontknow, 10);

if(trueselection==1){
    printf("Enter temperature in Fahrenheit: ");
    scanf("%f",&fahrenheit);
    celsius= (fahrenheit - 32) / 1.8;
    printf("Temperature in Celsius: %.2f\n\n\n",celsius);
}
else if(trueselection==2){
    printf("Enter temperature in Celsius: ");
    scanf("%f",&celsius);
    fahrenheit= (celsius*1.8)+32;
    printf("Temperature in Fahrenheit: %.2f\n\n\n",fahrenheit);
}
else if(trueselection==3){ //This "else if" statement is so the program does not say invalid selection before exiting if 3 is entered. 
return 0;
}
else{
    printf("Invalid selection !!!\n\n\n");
}
}
while(trueselection!=3); //This line tells the program to repeat back to             line 3 if any selection but 3 is selected. 
}

最佳答案

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

int main()
{
    float fahrenheit,celsius;
    int selection;
    char *idontknow;
    long trueselection;

    do{
        printf("1: Fahrenheit to Celsius."); 
        printf("\n2: Celsius to Fahrenheit.");
        printf("\n3: Quit program.\n");
        scanf("%d",&selection);

        if(trueselection==1){
            printf("Enter temperature in Fahrenheit: ");
            scanf("%f",&fahrenheit);
            celsius= (fahrenheit - 32) / 1.8;
            printf("Temperature in Celsius: %.2f\n\n\n",celsius);
        }
        else if(trueselection==2){
            printf("Enter temperature in Celsius: ");
            scanf("%f",&celsius);
            fahrenheit= (celsius*1.8)+32;
            printf("Temperature in Fahrenheit: %.2f\n\n\n",fahrenheit);
        }
        else if(trueselection==3){ //This "else if" statement is so the program does not say invalid selection before exiting if 3 is entered. 
            break;
        }
        else{
            printf("Invalid selection !!!\n\n\n");
        }
    }
    while(trueselection != 3); //This line tells the program to repeat back to line 3 if any selection but 3 is selected. 
}

关于c - 我不明白为什么我的 C 程序运行一次正确,然后却给出了不需要的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46249563/

相关文章:

c - 如何在c中使用libxml2根据属性对xml文件进行分类

c - 错误 "undefined reference to cvCreateFileCapture"

C:链表正确使用malloc

c - sinf() 比 <math.h> 中的 sin() 快?

c - BIO_new_mem_buf 未能创建 EVP_PKEY key ``

c - 尝试在 Linux 上用 C 语言制作一个简单的数据包路由器。 select() 无限期闲置,我不知道为什么

c++ - 如何使进程忽略某些信号(如 SIGHUP、SIGABRT、SIGABORT、SIGINT 等)

c - C 结构属性的默认值

C、数据结构

c - 在c中输入动态结构数组的malloc函数中的元素数量