Thanks for the tips! I appreciate it! Some of it was alittle too far advanced for me, but i was able achieve some progress im almost finished i think.
I changed my datafile, hopefully to simplify the procedure - i only put two columns of numbers. Column #1 = customer_number and Column #2 = gallons_used.
Example datafile.txt:
10 50
22 20
70 800
100 400
250 60
I used your advise and went with fgets() & sscanf(). I also put in a counter which allowed me to print out the total number of customers.
Code:
#include <stdio.h>
#include <string.h>
int main()
{
int customer_number, gallons_used, amount_due;
int CustomerCount = 0;
char str[255];
FILE *fpr = fopen("datafile.txt", "rt");
if( fpr == NULL )
return 1;
printf("Customer Number\t\tWater Usage\t\tAmount Due\n");
while( feof( fpr ) == 0 )
{
fgets(str, sizeof(str), fpr);
if(sscanf( str, "%d %d", &customer_number, &gallons_used) != 2)
break;
CustomerCount++;
printf("%d\t\t\t%d\n", customer_number, gallons_used);
}
fclose(fpr);
printf("\nThere are %d customers.\n", CustomerCount);
return 0;
}
and that produces an output of:
Code:
Customer Number Water Usage Amount Due
10 50
22 20
70 800
100 400
250 60
There are 5 customers.
Now i only need to accomplish three more objectives.
#1) How do i add the numbers in column two, so that i can figure out the total amount of consumed water.
#2) How do i take the data from column two and multiply it by a set price? So i can find the Amount Due for each costomer. For example lets say i now charged:
Water consumed 000 to 100 gallons = $0.10 per each gallon.
Water consumed 101 to 200 gallons = $0.20 per each gallon.
Water consumed 201 to 999 gallons = $0.30 per each gallon.
I know i would need a simple loop kinda like this:
if (gallons_used >= 999)
charge = .30
else if (gallons_used >= 200)
charge = .20
else (gallons_used >= 100)
charge = .10
But I dont know how to point towards column twos gallons used information. And i dont where or how i would put this code.
#3) Once i figure out each customers amount due, i need to find the total amount due from all the customers - in other words find a way to add up all everything in column 3 (amount due). Basically repeating objective number 1.
Also do i need to change the datafile.txt back to where i had it before or will the simplied version work fine too? Thanks for any help!!