Reputation: 1
Consider the file,
L,12/5/2008,Blacktown
C,Willy Wonker,10.00
C,Adolph Hitler,20.00
C,Attila the Hun,30.00
C,Idi Amin,40.00
C,Ghengis Khan,50.00
T,150.00
L,13/5/2008,Parramatta
C,Attila the Hun,100.10
C,Willy Wonker,200.20
C,Ghengis Khan,300.30
T,600.60
L,14/5/2008,Mount Druitt
C,Adolph Hitler,1000.00
T,2000.00
L,15/5/2008,Penrith
T,0.00
L,16/5/2008,Chatswood
C,Ghengis Khan,1.00
C,Idi Amin,10.00
C,Adolph Hitler,100.00
C,Attila the Hun,1000.00
T,1111.00
I need to write a program that opens the file Collections.txt
, reads it and formats it in the given order, and writes it to another file called Reports.txt
. I need to have the date
, a tab
, and then the name
. Then a new line and the names
, a tab
, and the amount
. Repeat until a T
is detected; this signals we have no more collections for the above location. Then rinse and repeat for every location until it reaches the end of the file and a final total must be displayed.
Here is the code I have written,
/*
This program collects data and writes it to a specific file.*/
#include <stdio.h>
#include <stdlib.h>
void debt_calculator();
void main()
{
printf("This is a debt collection program:\n ");
debt_calculator();
scanf("%*c");
}
void debt_calculator()
{
char code;
int date [40];
char location [40];
char name [40];
float amount = 0;
float total = 0;
float grand_total;
int ctr = 0;
FILE * Collections;
Collections=fopen("C:\\Users\\Nick\\Documents\\Visual Studio 2010\\Projects\\Assignment2\\Assignment2\\Collections.txt","r");
if(Collections==NULL)
{
printf("file not open:\n");
exit(1);
}
FILE * Report;
if((Report=fopen("C:\\Users\\Nick\\Documents\\Visual Studio 2010\\Projects\\Assignment2\\Assignment2\\Report.txt","w"))==NULL)
{
printf("can not open file: \n");
exit(1);
}
fscanf(Collections,"%c,",&code);
while(code == 'L')
{
fscanf(Collections,"%[^,],%s%*c",date,location);
fprintf(Report,"Date: %s \t Location: %s \n\n",date, location);
fscanf(Collections,"%c,",&code);
}
for(code == 'C'; ctr < 5; ctr++)
{
fscanf(Collections,"%[^,], %f %*c",name, & amount);
if(amount == 0)
{
fprintf(Report,"### No Collections for %s \n", location);
fprintf(Report,"total: \t 0.00\n");
}
fprintf(Report,"\n Name: %s \t Amount: %.2f \n",name, amount);
fscanf(Collections,"%c,",&code);
}
code == 'T';
while(code == 'T')
{
fprintf(Report,"Hello");
fscanf(Collections,"%f",& amount);
total += amount;
grand_total += total;
fprintf(Report,"Total: \t %.2f \n",& total);
fprintf(Report,"Grand Total of all Collections is: $ %.2f", grand_total);
fscanf(Collections,"%c" , & code);
}
fclose(Collections);
fclose(Report);
}
When I run the above program it displays the printf()
statement and when I hit enter and it exits. Than I open my report.txt
file and find the date tab location new line and all the names and costs up until the T
. Once it gets to the 'T' it stops, I cant get it to go into the last while loop, read the total and print it to the file than repeat by checking the code again. The code after the T
is a L
so it should go back into the first while loop and read than write the date and location but it does not. Any ideas what I'm doing wrong?
Also if there's any better way you think I should be doing this please let me know.
Upvotes: 0
Views: 894
Reputation: 93456
Have you tries stepping in the debugger and observing the variable state while doing so?
This line:
fprintf(Report,"Total: \t %.2f \n",& total);
is certainly incorrect, %f does not expect a pointer.
If the expectation is that code == 'T';
will force it to enter the loop, you are mistaken, code = 'T';
would work.
Similarly in this loop:
for(code == 'C'; ctr < 5; ctr++)
The initialising expression shpuld be code = 'C'
Try setting your compiler warning level to \W4 and \Wx (all warnings are errors), then the compiler may help you spot such errors.
Upvotes: 1