Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.
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;
}
 
In your if (text == 1) statement you have opened the file in read only mode (r) rather than append (r+).

That is the only thing I have noticed from a cursory glance.

thanks. maybe i should make it like that in the others.

the problems are in if(text == 2) and if(text == 3) statements
 
Well, you have a bunch of logic issues around the file handling and your looping that it was easier to show you then to tell you in this case.

You should have left the switch in, it looks cleaner. Other than what others have told you about the scanf, you needed to test if this was the first data you had in the file. If the file was empty, obviously you can't read it.

The other aspect of this was you only want one record in the file, so you essentially keep re-writing the first record. That's the rewind() that you see, it sends the file pointer back to the top of the file after you've done a read.

Here's a small code example of the first menu choice. I'll leave you to figure out the rest. If you need help or more of an explanation, just ask...

Code:
#include <stdio.h>
#include <stdlib.h>

int main (int argc, const char * argv[]) {
    float miles;
	float totalMiles;
	int text;
	
	FILE *fp; 

	do
	{
		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");

		text = 0;

		scanf ("%i", &text);

		if ( (text < 1) || (text > 5)) {
			printf("Valid Choices are 1 - 5\n");
			continue;
		}
		
		if (text == 1)
		{
			printf ("Hello, enter miles driven- ");
			scanf ("%f", &miles);
	
			if ( (fp = fopen("miles.txt","r+")) != NULL ){
				fscanf(fp, "%f", &totalMiles);
				totalMiles = miles + totalMiles;
				printf ("Total Miles: %.2f\n", totalMiles);

				rewind(fp);
				fprintf(fp, "%.2f", totalMiles); /*writes*/
				fclose(fp);
			}
			else // File doesn't exist yet
			if ( (fp = fopen("miles.txt","w")) != NULL ){
				printf ("Total Miles: %.2f\n", miles);
				fprintf(fp, "%f", miles);
				fclose(fp);
			}
			else
				printf("Failed to Open miles.txt\n");
		}
		
	}while (text != 5);

	printf("Bye Bye...\n");
   
    exit(0);
}
 
Well, you have a bunch of logic issues around the file handling and your looping that it was easier to show you then to tell you in this case.

You should have left the switch in, it looks cleaner. Other than what others have told you about the scanf, you needed to test if this was the first data you had in the file. If the file was empty, obviously you can't read it.

The other aspect of this was you only want one record in the file, so you essentially keep re-writing the first record. That's the rewind() that you see, it sends the file pointer back to the top of the file after you've done a read.

Here's a small code example of the first menu choice. I'll leave you to figure out the rest. If you need help or more of an explanation, just ask...

thanks for the help! i'll try to finish the rest.
 
well i think i got it all working. thanks to everyone for the help!

would it be smarter to put the data in the file in an array or matrix? can i have a matrix that has different data types?
 
well i think i got it all working. thanks to everyone for the help!

would it be smarter to put the data in the file in an array or matrix? can i have a matrix that has different data types?

or maybe a core data application? i know it's not command-line programming, but it seems to save data easier
 
thanks. well, i've been looking at core data apps, but i don't see how to add, multiply, and divide and all. do you know how to do that?

Not sure of the question. Add, divide or multiply what? I assume it is just standard maths I have no idea about core data at all.

The database I was refering to was the dbm database. Type man dbm for more information.
 
Not sure of the question. Add, divide or multiply what? I assume it is just standard maths I have no idea about core data at all.

The database I was refering to was the dbm database. Type man dbm for more information.

numbers, mainly floats. just like in this application i made.

thanks, i'll look into the dbm database when i get home
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.