1

I need to write C program that reads from file the car number, miles driven, and gallons used. Calculate the miles per gallon. Calculate the totals and the average MPG.

I only need help with counting miles per gallon.

In output should be :20    But my output is: 1966018914
                     25                      20
                     24                      25
                     23                      24

Can anyone see my code and help me figure it out?!

Here is code:

int main()
{
    int car, miles, gas;
    int sumMiles = 0;
    int sumGas = 0;
    int avgMPG = 0;
    FILE *inFile, *outFile;
    char fname[20];

    printf("Enter a file name: ");
    gets(fname);

    inFile = fopen(fname, "r");
    if (inFile == NULL)
    {
        printf("\nFailed to open file.\n");
        exit(1);
    }

    outFile =  fopen("output.txt","w");

    if(outFile==NULL)
    {
        printf("The file was not opened.");
        exit(1);
    }

    printf("\nCar No.   Miles Driven    Gallons Used\n");

    while (fscanf(inFile, "%d %d %d",&car, &miles, &gas) != EOF)
    {
        printf("%-7d %-15d %d\n",car,miles,gas);
        sumMiles += miles;
        sumGas += gas;
        avgMPG = sumMiles / sumGas;
    }

    printf("\nThe total miles driven is %d\n", sumMiles);
    printf("The total gallons of gas used is %d\n", sumGas);
    printf("The average miles per gallon of gas used is %d\n", avgMPG);

    printf("File copied succesfully!");
    fclose(inFile);
    fclose(outFile);

}

This is input file:

123 100 5
345 150 6
678 240 10
901 350 15
user2884834
  • 129
  • 4
  • 9
  • 14
  • 1
    Hmm, I don't even see where you're writing to `outFile`. Also, what does the output look like with your `printf` statements? – Michael Nov 19 '13 at 07:47
  • 1
    where do you calculate the miles per gallon? – kunal Nov 19 '13 at 07:48
  • 1
    Where did you get this output? Your program outputs something totally different. – n. m. could be an AI Nov 19 '13 at 07:49
  • Please burn your source of learning C and get a new one. You are learning C programming as if it's the 1980s. [Why is the gets function so dangerous that it should not be used?](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) – Lundin Jun 28 '18 at 06:15

3 Answers3

0

Your program is working just fine: IdeOne demo.

All I did to your code is to remove input and output files and replace them with stdin and stdout which is required by IdeOne. Also, you do not seem to use output file anywhere.

mvp
  • 111,019
  • 13
  • 122
  • 148
0

Input:

123 100 5
345 150 6
678 240 10
901 350 15

Output:

Car No.   Miles Driven    Gallons Used
123     100             5
345     150             6
678     240             10
901     350             15

The total miles driven is 840
The total gallons of gas used is 36
The average miles per gallon of gas used is 23

Your code works with everyone else but you. The issue may be in the environment then. Do you have an exotic encoding in your text editor? Are you leaving stray characters, indentation etc?

erbdex
  • 1,899
  • 3
  • 22
  • 40
-1

Your code is working fine. You have been using printf in code which will not work it want to write output to a file you need to use fprint. Please have a look at below code which works as you need.

int main()
{
int car, miles, gas;
int sumMiles = 0;
int sumGas = 0;
int avgMPG = 0;
float ans=0;
FILE *inFile, *outFile;
char fname[20];

printf("Enter a file name: ");
gets(fname);

inFile = fopen(fname, "r");
if (inFile == NULL)
{
    printf("\nFailed to open file.\n");
    exit(1);
}

outFile =  fopen("output.txt","w");

if(outFile==NULL)
{
    printf("The file was not opened.");
    exit(1);
}

while (fscanf(inFile, "%d %d %d",&car, &miles, &gas) != EOF)
{
    sumMiles += miles; // not needed acc to output
    sumGas += gas; // not needed acc to output
    avgMPG = sumMiles / sumGas; // not needed acc to output
    ans = miles/gas;
    fprintf(outFile,"%f\n",ans);
}

fclose(inFile);
fclose(outFile);

}
Vishal R
  • 1,026
  • 1
  • 7
  • 21