ok, the program isn't working. when i enter the miles, it works, but i get a bus error for gallons, and another error for price. here is the code:
Code:
#include <stdio.h>
int main (int argc, const char * argv[]) {
float miles;
float gallons;
float price;
float mpg;
float ppm;
float totalMiles;
float totalGallons;
float totalPrice;
int text;
printf ("Hello, what would you like to do?\n");
printf ("1 - enter miles\n");
printf ("2 - enter gallons\n");
printf ("3 - enter price\n");
printf ("4 - see summary\n");
printf ("5 - exit\n");
while (scanf ("%i", &text) != 5)
{
if (text == 1)
{
printf ("Hello, enter miles driven- ");
scanf ("%f", &miles);
FILE *file;
file = fopen("miles.txt","r"); /* apend file (add text to
a file or create a file if it does not exist.*/
fscanf(file, "%f", &totalMiles); /*writes*/
fclose(file); /*done!*/
totalMiles = miles + totalMiles;
printf ("%.2f", totalMiles);
FILE *file1;
file1 = fopen("miles.txt","w+"); /* apend file (add text to
a file or create a file if it does not exist.*/
fprintf(file1, "%.2f", totalMiles); /*writes*/
fclose(file1); /*done!*/
}
if (text == 2)
{
printf ("Hello, enter gallons used- ");
scanf ("%f", &gallons);
FILE *file2;
file2 = fopen("gallons.txt","r+"); /* apend file (add text to
a file or create a file if it does not exist.*/
fscanf(file2, "%f", totalGallons); /*writes*/
fclose(file2); /*done!*/
totalGallons += gallons;
FILE *filea;
filea = fopen("gallons.txt","w+"); /* apend file (add text to
a file or create a file if it does not exist.*/
fprintf(filea, "%.3f", gallons); /*writes*/
fclose(filea); /*done!*/
}
if (text == 3)
{
printf ("Hello, enter price paid- ");
scanf ("%f", &price);
FILE *file3;
file3 = fopen("price.txt","r+"); /* apend file (add text to
a file or create a file if it does not exist.*/
fscanf(file3, "%f", totalPrice); /*writes*/
fclose(file3); /*done!*/
totalPrice += price;
FILE *file4;
file4 = fopen("price.txt","w+"); /* apend file (add text to
a file or create a file if it does not exist.*/
fprintf(file4, "%.2f", price); /*writes*/
fclose(file4); /*done!*/
}
if (text == 4)
{
mpg = totalMiles/totalGallons;
ppm = totalPrice/totalMiles;
printf("You drove %.2f miles.\n", totalMiles);
printf("You used %.3f gallons.\n", totalGallons);
printf("You paid $%.2f\n", totalPrice);
printf("You got %.2f miles per gallon.\n", mpg);
printf("You got $%.2f per mile\n", ppm);
FILE *file5;
file5 = fopen("summary.txt","w+"); /* apend file (add text to
a file or create a file if it does not exist.*/
fprintf(file5, "You drove %.2f miles.\n", totalMiles); /*writes*/
fprintf(file5, "You used %.3f gallons.\n", totalGallons); /*writes*/
fprintf(file5, "You paid $%.2f\n", totalPrice); /*writes*/
fprintf(file5, "You got %.2f miles per gallon.\n", mpg); /*writes*/
fprintf(file5, "You got $%.2f per mile\n", ppm); /*writes*/
fclose(file5); /*done!*/
}
}
return 0;
}