I Have a C++ Assignment I need help with. Please advise.

Write your question here.

Write a C++ program that the Traffic Department can use when the owner of a car pays all his
outstanding road fines. The Traffic Department has a file called Fines.dat that keeps the
registration number and the road fine due for each traffic offence, one per line.
Your program should request the registration number from the user, read the contents of file
Fines.dat (shown below) line by line; if the road fine matches the registration number, display
the fine and calculate the total amount due for that registration number. At the same time all the
remaining registration numbers and road fines should be written to a new file called
OutStandingFines.dat.
Run your program to calculate the road fines owed by a car with registration number ABC123.
Fines.dat:
ABC123 400
DEC234 340
ABC123 500
GED345 600
ABC123 240
GEE600 120
GED345 230
GEE600 470
ABC123 120
NB: You should not count the number of lines in the file to determine how many times the file
should be accessed.
Sample output:
Please enter registration number: ABC123
Fines:
R400.00
R500.00
R240.00
R120.00
Total fine due R1260.00

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <fstream>
using namespace std;

int main()

{
    //Write info to .dat file:
    ofstream FineFile1("Fines.dat");

    FineFile1<<"ABC123 400\n"<<endl;
    FineFile1<<"DEC234 340\n"<<endl;
    FineFile1<<"ABC123 500\n"<<endl;
    FineFile1<<"GED345 600\n"<<endl;
    FineFile1<<"ABC123 240\n"<<endl;
    FineFile1<<"GEE600 120\n"<<endl;
    FineFile1<<"GED345 230\n"<<endl;
    FineFile1<<"GEE600 470\n"<<endl;
    FineFile1<<"ABC123 120\n"<<endl;

    ifstream FineFile("Fines.dat");

    string RegNum;
    int FineAmmount,next =0,counter, sum = 0;

    cout<<"Please enter regestration number: ";
    cin>> RegNum;

    while( FineFile >> RegNum >> FineAmmount)
   {
       if(RegNum == "ABC123")
        {
       cout<<"R "<< FineAmmount<<endl;
        }



    }
//Sum the amount of all numbers desplayed on screen.
while(FineAmmount >> next)
    {
        sum = sum + next;

        counter ++;
    }

    cout<<sum;



    //Writes Other Fines Execpt ABC123 to OutStandingFines file:
     ofstream OutFineFile("OutStandingFines.dat.");

    OutFineFile<<"DEC234 340\n"<<endl;
    OutFineFile<<"GED345 600\n"<<endl;
    OutFineFile<<"GEE600 120\n"<<endl;
    OutFineFile<<"GED345 230\n"<<endl;
    OutFineFile<<"GEE600 470\n"<<endl;



    return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
you should close the file here:

  FineFile1<<"ABC123 120\n"<<endl;
    FineFile1.close();
    ifstream FineFile("Fines.dat");



  cin>> RegNumTARGET;   //should this block read into a string that you use to compare against instead of hard coded ABC123?!

    while( FineFile >> RegNum >> FineAmmount)
   {
       if(RegNum == RegNumTARGET)  //like so?

hardcoding the output file for the others is probably not intended by your assignment. 
instead, they probably want logic like this:

       if(RegNum == RegNumTARGET)  //like so?
        ...
       else 
          {
             write that line to the output file

Last edited on
Thank you so much for your help. But how would that else statement look like?
Registered users can post here. Sign in or register to post.