[Solved] Variable is uninitialized whenever function ‘main’ is called in C [duplicate]


Well the message is clear and it is easy to spot in your program:

double gallons,miles;

while (miles>=0||gallons>=0) {

miles is declared in a function and so is an automatic variable. Automatic variables are not initialized (so they have garbage values). Now in the first executable statement you compare miles. But miles is still uninitialized. Maybe copy the following lines to before the while? Same for gallons.

    printf("Enter the miles driven: ");
    scanf("%lf",&miles);

Note: check the return value of scanf if indeed a value was read.

BTW, you could take the habit of initializing your local variables, e.g. with double gallons = 0.0, miles = 0.0; instead of just declaring double gallons,miles;.

1

solved Variable is uninitialized whenever function ‘main’ is called in C [duplicate]