-1

I was wondering how to get rid of the 1.#J output that appears when it is run. I know it is related to the float mpg variable after the trip selection part. I am just beginning to use C, so any help is greatly appreciated. Also, the program is not yet finished.

#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;
}
phuclv
  • 37,963
  • 15
  • 156
  • 475
AMM24
  • 7
  • 1
  • 1.#J happens when you round 1.#INF to two decimal digits. You get infinity by dividing by 0. That extra mpg variable is not initialized, delete it. – Hans Passant Apr 11 '16 at 16:18

1 Answers1

0

There are so many problems with the code. Why are you declaring so many float mpgs? mpg in the while loop will shadow the global version. Moreover in the trip selection part:

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

You're using mpg when it's not initialized and that invokes undefined behavior. The garbage in mpg can return anything including 1.#J. Same to other mpg variables in the other if blocks because it's the global one which was not initialized.

Another problem:

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

That doesn't do what you think and the program will loop forever. Read how the comma operator work first. Change the loop to while (trip >= 1 && trip <= 5)

Community
  • 1
  • 1
phuclv
  • 37,963
  • 15
  • 156
  • 475