Reading Files (Income Tax)

I need help trying to figure out why my data is not being displayed in my text box. I have no compiling errors and it seems as if my program isnt doing anything.
Here is my code:

#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

const double FICA = 0.18;
const double SOCIAL_SECURITY = 0.07;

int main()
{

string f_name;
string l_name;
double gross;
char dollar_sign;
double tax_amount;
double social_amount;
double fica_amount;


ifstream inFile;
ofstream outFile;

inFile.open ("nc_incomes2.txt");
outFile.open ("nc_netSalaries.dat");

if (!inFile)
cout << "Error opening file.\n";

else
{
while (!inFile.eof())
{

getline(inFile, f_name);
getline(inFile, l_name);
inFile >> gross;

}

fica_amount = gross * FICA;
social_amount = gross * SOCIAL_SECURITY;
tax_amount = social_amount + fica_amount;

outFile << fixed << showpoint << setprecision(2)

<< " Income Tax Report \n\n"
<< "First Name: " << f_name << "\n"
<< "Last Name: " << l_name << "\n\n"
<< setfill('.') << left << setw(31)
<< "Gross:"
<< setfill(' ') << right << setw(12)
<< gross << "\n"
<< "FICA:" << " $"
<< setfill(' ') << right << setw(10)
<< fica_amount << "\n"
<< setfill('.') << left << setw(31)
<< "Social Security Tax:"
<< setfill(' ') << right << setw(12)
<< social_amount << " %\n\n"
<< setfill('.') << left << setw(31)
<< "Taxed Amount:" << " $"
<< setfill(' ') << right << setw(10)
<< tax_amount << "\n"
<< setfill('.') << left << setw(31)
<< endl;
system("pause");
}


inFile.close();
outFile.close();

return 0;
}
I had no idea what kind of stuff to put in ncincomes.txt, so I just threw in some random integers. This was my program output:
 Income Tax Report 

First Name: 
Last Name: 53

Gross:.........................        5.00
FICA: $      0.90
Social Security Tax:...........        0.35 %

Taxed Amount:.................. $      1.25


Does that look anything like what you expected?
First of all, use code tags. "[c o d e]// your code[/c o d e]" (without the spaces between the letters)

This works:
1
2
if (!inFile) 
cout << "Error opening file.\n";


but you never "create" these files. You may want to have the user input a name of their choice using cin or something.

Assuming you have the .txt and .dat files pre-made, I think the problem may be: while (!inFile.eof())
If it encounters an eof flag before you have even done anything it will exit the loop before it begins. You could test the stream before entering the loop but after creating it with something like this:
std::cout << " eofbit:"; print_state(stream); std::cout << '\n';

Also I'm a little surprised it compiles at all with so many syntax errors. Must be a miraculous combination of errors that messed it up so badly that the compiler now thinks it's right.
All of this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
outFile << fixed << showpoint << setprecision(2)

<< " Income Tax Report \n\n"
<< "First Name: " << f_name << "\n"
<< "Last Name: " << l_name << "\n\n"
<< setfill('.') << left << setw(31) 
<< "Gross:" 
<< setfill(' ') << right << setw(12)
<< gross << "\n"
<< "FICA:" << " $"
<< setfill(' ') << right << setw(10)
<< fica_amount << "\n"
<< setfill('.') << left << setw(31)
<< "Social Security Tax:" 
<< setfill(' ') << right << setw(12)
<< social_amount << " %\n\n"
<< setfill('.') << left << setw(31)
<< "Taxed Amount:" << " $"
<< setfill(' ') << right << setw(10)
<< tax_amount << "\n"
<< setfill('.') << left << setw(31)
<< endl;

Is heavily problematic. First of all, it does nothing, let alone what you want it to do. You aren't doing anything to any stream because you aren't even trying to. I can't figure out why all of this does not throw a compiler error...

You even missing basic things like semi-colons on most lines. I assume that these are all supposed to be cout lines so you should use cout

Before I look at too much more than that it might be useful if you take another look at your own program after reading the following reference section: http://www.cplusplus.com/doc/tutorial/files/


and probably: http://www.cplusplus.com/reference/fstream/ifstream/?kw=ifstream

Hopefully something there might help you,
Sean
Also I'm a little surprised it compiles at all with so many syntax errors. Must be a miraculous combination of errors that messed it up so badly that the compiler now thinks it's right.


This made me crease.
First of all, it does nothing, let alone what you want it to do. You aren't doing anything to any stream because you aren't even trying to. I can't figure out why all of this does not throw a compiler error...

You even missing basic things like semi-colons on most lines. I assume that these are all supposed to be cout lines so you should use cout

Um, slambert, that bit of code you quoted is all one statement. Just cause it's been broken up with newlines, doesn't stop it being a single statement.

Look at line 1 of your snippet - the OP is clearly streaming stuff into outFile, which has been declared as an ofstream. Each of the lines you've quoted after that is a continuation of the same statement. It's perfectly legal and, as far as I can see, perfectly functional.
Last edited on
Topic archived. No new replies allowed.