Take the 2-minute tour ×

Consider the file,

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
C,Attila the Hun,100.10
C,Willy Wonker,200.20
C,Ghengis Khan,300.30
L,14/5/2008,Mount Druitt
C,Adolph Hitler,1000.00
C,Ghengis Khan,1.00
C,Idi Amin,10.00
C,Adolph Hitler,100.00
C,Attila the Hun,1000.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 ");

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");
        printf("file not open:\n");

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


    while(code == 'L')
        fprintf(Report,"Date: %s \t Location: %s \n\n",date, location);

    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);

    code == 'T';
    while(code == 'T')
        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);


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.

share|improve this question
I don't see any C++ in there. Changed tag to C. –  Billy ONeal Nov 12 '10 at 13:31
Debugger. Do you use it? –  n0rd Nov 12 '10 at 13:34
@Jonathan: "The homework tag...is now discouraged," but, @Nick, please (as always) follow general guidelines: state any special restrictions, show what you've tried so far, and ask about what specifically is confusing you. –  Roger Pate Nov 12 '10 at 14:33
@Billy: I think that was caused by entering "visual c++" in the tags for the IDE he's using (i.e. note the original title). –  Roger Pate Nov 12 '10 at 14:39
@Roger Thanks, I hadn't been aware of that. –  Jonathan Nov 15 '10 at 16:09

1 Answer 1

Have you tries stepping in the debugger and observing the variable state while doing so?

This line:


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:


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.

share|improve this answer
I'd like an explanation for the down-vote!? –  Clifford Nov 12 '10 at 13:48
+1, and I'd suggest that putting code = 'C' in the initialisation part of a loop when it is not in the test part is loop abuse. –  Martin Broadhurst Nov 12 '10 at 13:57
Hi.For the for loop thats a typo i realize why it should be one = instead of 2. But for the while loop it should be 2 not one should it not? because one = is saying the variable is this, where 2 is saying is the variable this. thats what i need to check, i cant say code = 'T' because that would be a automatic fail because i am not reading the file, im telling the program what it should do when it should be the one figuring out that to do. I think what our teacher wants us to do is use a switch case statement for each case. Would that be a good idea? If so how would i implement it? –  Nick Nov 13 '10 at 6:28
Yes i have tried the debugger, our teacher always tells us to ignore warnings because the compiler gives us stupid warnings like telling us to use gets when im storing a char not a string. –  Nick Nov 13 '10 at 6:36
@Nick: That is very poor advice from your teacher, and suggests that he has poor knowledge of programming; those warnings are the compiler helping you find bugs, even when they are benign, they should be fixed if only so you can see the wood for the trees and not miss real semantic errors . I doubt the compiler gave any such warning as "use gets when storing a char not a string". You should never use gets() in any case! –  Clifford Nov 13 '10 at 8:53

Your Answer


