如何摆脱此代码中的#J输出?

时间:2015-05-28 17:54:26

标签: c floating-point-conversion

我想知道如何摆脱运行时出现的1.#J输出。我知道它与行程选择部分之后的浮动mpg变量有关。我刚刚开始使用C,所以非常感谢任何帮助。此外,该计划尚未完成。

#include <stdio.h>
#define MAX_LEN 80

float mpg, distance;

int main ( )
{
    int name[MAX_LEN];
    printf("Hello! Can you please enter your name: ");
    scanf("%s", name);
    printf("Okay, %s, Let's begin!\n", name);

    char answer;
    printf("%s, do you own a car? (Y or N):");
    scanf(" %c", &answer);
    while (answer == 'y' || answer == 'Y')
    {
        printf("Great, welcome to the road trip calculator.\n");
        printf("\n");
        printf("First we will need to know somethings about your car.\n");

        float gal_to_fill;

        printf("About how many gallons of gas does it take to fill it up: ");
        scanf("%f", &gal_to_fill);
        printf("%.2f\n",gal_to_fill);

        float mpg;
        printf("About how many miles can you drive per gallon: ");
        scanf("%f", &mpg);
        printf("%.2f\n",mpg);

        printf("Okay, now let's pick a location to travel to.\n");
        printf("We are going to start at ________\n");
        printf("Please pick a final destination:\n");
        printf("1. Boston, MA\n");
        printf("2. New York, NY\n");
        printf("3. Miami, FL\n");
        printf("4. Chicago, IL\n");
        printf("5. San Francisco, CA\n");
        break;
    }

    int trip;
    printf("Enter the number of your choice: ");
    scanf("%d", &trip);

    while(trip == 1,2,3,4,5)
    {
        if (trip == 1)
        {
            float mpg;
            float distance = 141.9;
            printf("This trip is 141.9 miles\n");
            float gal_used;
            gal_used = distance/mpg;
            printf("%.2f\n",gal_used);
            break;
        }
        else if (trip == 2)
        {
            float distance = 343.6;
            printf("This trip is 343.6 miles\n");
            float gal_used;
            gal_used = distance/mpg;
            printf("%.2f\n", gal_used);
            break;
        }
        else if (trip == 3)
        {
            float distance = 1623.3;
            printf("This trip is 1623.3 miles\n");
            float gal_used;
            gal_used = distance/mpg;
            printf("%.2f\n", gal_used);
            break;
        }
        else if (trip == 4)
        {
            float distance = 1112.8;
            printf("This trip is 1112.8 miles\n");
            float gal_used;
            gal_used = distance/mpg;
            printf("%.2f\n", gal_used);
            break;
        }
        else if (trip == 5)
        {
            float distance = 3227.2;
            printf("This trip is 3227.2 miles\n");
            float gal_used;
            gal_used = distance/mpg;
            printf("%.2f\n", gal_used);
            break;
        }
    }
    return 0;
}

1 个答案:

答案 0 :(得分:0)

代码存在很多问题。你为什么宣布这么多float mpg? while循环中的mpg将影响全局版本。此外,在旅行选择部分:

float mpg;
float distance = 141.9;
printf("This trip is 141.9 miles\n");
float gal_used;
gal_used = distance/mpg;

如果未初始化并调用未定义的行为,则会使用mpg。 mpg中的垃圾可以返回任何内容,包括1.#J。与其他if块中的其他mpg变量相同,因为它是未初始化的全局变量。

另一个问题:

while(trip == 1,2,3,4,5)

这不符合您的想法,程序将永远循环。首先阅读how the comma operator work。将循环更改为while (trip >= 1 && trip <= 5)