0

私はこれを自分でデバッグする必要があることを知っています...しかし、私が試したことを信じてください。私は非常に恥ずかしいです。while ループが無限にループしている理由がわかりません。誰でも助けることができますか?

#include <stdio.h>

int main ( void )
{
    double milesDriven;
    double gallonsUsed;
    double totalMilesDriven;
    double totalGallonsUsed;
    float milesPerGallon;
    float totalMpG;

printf( "%s", " Enter the gallons used (-1 to end): " );
scanf( "%i", &gallonsUsed);

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


while ( gallonsUsed != -1 || milesDriven != -1)
{
     totalGallonsUsed += gallonsUsed;
     totalMilesDriven += milesDriven;

     milesPerGallon = ( milesDriven / gallonsUsed );
     printf( " The miles/gallon for this tank was %f\n", milesPerGallon );

     printf( "%s", " Enter the gallons used (-1 to end): " );
     scanf( "%i", &gallonsUsed);

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

}


totalMpG = ( totalMilesDriven / totalGallonsUsed );
printf( " The overall average miles/gallon was %.6f\n ", totalMpG); 
return 0;    
}
4

2 に答える 2

4

一見すると、整数を使用する必要があるときに、浮動小数点データ型を使用しているように見えます。

"%i" // expects an integer

データ型を使用してみるintか、フォーマットを次のように変更してください"%lf"

于 2013-09-18T01:12:45.893 に答える