My code does not read the .txt file.

My teacher gave me the following instructions:

A. Open a text file and type at least ten records (each full record of an account should be on a separate line). Each record will consist of the following items separated by spaces:

1- Credit Card (16 digits – no dashes or spaces. You should use a string variable for credit card number).
2- First Name.
3- Last Name.
4- Interest Rate (between 11.4% and 24.9%)
5- Balance from Previous statement.
6- Current charges.
Save the file as “balances.txt”.

B. Now write a C++ program that would read each record, calculate the interest charges on the previous balance for the current period (one month), add the current charges, calculate the new balance and write out the same record format with the newly calculated balance (and 0 current charges of course).

the information I created is:

John Smith, 8970624354603214, 12.4, 5600, 7200
Liam Jones, 6243534182163415, 11.7, 7600, 6321
Jackson Miller, 5312532162438241, 13.1, 4500, 5432
David Clark, 6032824132622432, 20.1, 3200, 8321
Jack Davis, 7281834132122462, 22.6, 8200, 7334
Olivia Scott, 2561324345634250, 17.2, 5750, 8326
Sophia Baker, 3242251562708256, 15.4, 6210, 6524
Emily Collins, 7241328475436270, 23.8, 5712, 8223
Amanda Evans, 7261326870352516, 16.3, 6320, 4321
Chloe Rogers, 6132423661403240, 18.2, 3525, 6748

and the code I have is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  #include <iostream> 
#include <fstream>
using namespace std;
int main () 
{
ifstream fin; 
ofstream fout;
string firstName, lastName; 
fin.open ("balances.txt");
fout.open ("balances2.txt");
fin >> firstName >> lastName >>
fout << firstName << ", " << lastName << "," << '\t'<< endl; 
<< " " << lastName << endl;
fin >> creditcard >> interest >> previousbalance >> currentcharges;
fout << creditcard: " ";
fout << interest: " ";
fout << previousbalance: " ";
fout << currentcharges: " "; 
fin.close ("balances.txt");
fout.close ("balances2.txt");
cout << "interest charges on previous balance for the curret period" << endl; 
return 0;
}
Last edited on
Your code doesn't even compile much less read your data file. For example how and where is creditcard defined?

First off, your code doesn't compile.
You need to correct the errors in your program before it will run.

Line 3: You're missing #include <string>

Line 11: Has extraneous >> and no ;

Line 13: Not sure what this fragment is supposed to do. I would say fout is missing, but you've already written lastName to fout at line 12.

Lines 14-18: These variables are all undefined.

Lines 15-18: Missing << before " ".

Line 19:20: close() does not take an argument

Two other observations:
1) The order of the data in your file does not match the order given by your teacher.
2) You're only going to read one record from the file. You need a loop in order to read records until you reach eof.
Last edited on
closed account (iw7Gy60M)
In addition to comments from others, I would point out that your input file doesn't meet the specifications of your assignment. The credit card number should be first and the fields should be separated by spaces. You have the name first and the fields are separated by commas.
Last edited on
Thank you all!
Topic archived. No new replies allowed.