程序将编译没有错误,但不会执行。怎么了? (在C中)

时间:2015-02-23 04:15:49

标签: c

因此,出于某种原因,每当我尝试在gedit中编译此程序时,它都可以正常工作(没有错误)。但是当我尝试用./prgram执行它时,它只是跳到终端的下一行。我不确定这个程序是不起作用的,还是与我的编译器有关。谢谢! (这是一项家庭作业,因此需要额外费用才能获得全额学分)

#include <stdio.h>
const double CEL = 32;
const double KEL = 459.67;

double FahToCel(double);
double FahToKel(double);

int main()
    {
    double cel;
    double kel;
    double faren;
    char i[1] = "a";
    while (i == "a")
    {
        printf("Enter a temperature in farenheit(enter s to stop):\n"); 
        scanf("%lf\n", &faren);
        printf("%lf Farenheit is equal to %lf Celsius, and %lf Kelvin\n", faren, FahToCel(cel), FahToKel(kel));
        if (kel <= -274)
            printf("Atomic motion has stopped\n");
        else if ((kel >= -274) && (kel <= 273))
            printf("Water freezes here\n");
        else if ((kel >= 274) && (kel <= 280))
            printf("You'll need a sweater\n");
        else if ((kel >= 281) && (kel <= 294))
            printf("A bit chilly\n");
        else 
            printf("Ahh, that's better\n");
    }
    return 0;
}

double FahToCel(double faren)
{
    double cel;
    cel = (faren - CEL) * 5/9;
    return cel;
}

double FahToKel(double faren)
{
    double kel;
    kel = (faren + KEL) * 5/9;
    return kel;
}

3 个答案:

答案 0 :(得分:1)

在您的计划中,while (i == "a")中的条件不会评估为TRUE。所以它不进入while循环,程序执行return 0并退出。

要比较字符串,您应该使用strcmp()

此外,char i[1] = "a";生成长度为1的字符数组,但您希望它长度为2个字符。一个用于'a',第二个用于'\0',以使其成为NULL终止字符串。

答案 1 :(得分:1)

在循环条件下你可以使用......这......

 while (i[0] == 'a')

这样它会检查单个字符....

答案 2 :(得分:0)

Click Here To Run Your Program

#include <stdio.h>
const double CEL = 32;
const double KEL = 459.67;

double FahToCel(double);
double FahToKel(double);
void clean_stdin(void);
int main()
    {
    double cel;
    double kel;
    double faren;
    char i = 'a';
    while (i != 'N')
    {
        printf("\nEnter a temperature in farenheit :\n"); 
        scanf("%lf", &faren);
        printf("%lf Farenheit is equal to %lf Celsius, and %lf Kelvin\n", faren, FahToCel(cel), FahToKel(kel));
        if (kel <= -274)
            printf("Atomic motion has stopped\n");
        else if ((kel >= -274) && (kel <= 273))
            printf("Water freezes here\n");
        else if ((kel >= 274) && (kel <= 280))
            printf("You'll need a sweater\n");
        else if ((kel >= 281) && (kel <= 294))
            printf("A bit chilly\n");
        else 
            printf("Ahh, that's better\n");
        printf("Do you want to Continue ? ( N : Stop) : \n"); 
        clean_stdin();
        scanf("%c",&i);
    }
    return 0;
}

double FahToCel(double faren)
{
    double cel;
    cel = (faren - CEL) * 5/9;
    return cel;
}

double FahToKel(double faren)
{
    double kel;
    kel = (faren + KEL) * 5/9;
    return kel;
}

void clean_stdin(void)
{
    int c;
    do {
        c = getchar();
    } while (c != '\n' && c != EOF);
}