net income program

Hello everyone so im having a little trouble with my program. Im supposed to create a program to read a file calculate the net income and print the full name, tax id, gross income, taxes deducted and net income.
what im having trouble with is skipping data that is invalid (gross income should be greater than 0 and tax id should be 1000-9999 if these are not true that person is skipped)

Last edited on
So the code you posted is incomplete. Your mistake would be in the while loop. Yet all you have is comments... The code you have posted would not produce the results you claim it gives. Once we see the real thing then we can help. Also it is a good idea to post a snippet of that input file so we can see how it is formatted and how your input operations would extract the data.

Now lets talk about that while loop as long as there are no extraction operations there it will run forever.... so I hope that is there...

From what you have given it seems the only place you test for the tax_id is in the input_data function. However you do not really do anything differently other then print a message to std::cout. How does this affect the rest of the program? there are no outputs from input_data that tell your code that this was an invalid result....
Last edited on
[code]#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;


int main()
{
string full_name;
int taxID;
double grossINCOME, taxedAMOUNT, netINCOME;


ifstream fin;
ofstream fout;

intro (); // Program introduction

if (!file_in_open(fin)) // check/open data file
{
cout<<"Bad Input File. Program Terminated. Try again later."<<endl;
exit (0);
}

else
{
if (!file_out_open(fout)) // check/open output file
{
cout<<"Bad Output File. Program Terminated. Try again later."<<endl;
exit (0);
}

cout<<left<<setw(15)<<"Name"<<left<<setw(10)<<"Tax ID"<<left<<setw(15)<<"Gross Income"<<left<<setw(15)<<"Taxes"<<left<<setw(10)<<"Net Income"<<endl;

fout<<left<<setw(20)<<"Name"<<left<<setw(15)<<"Tax ID"<<left<<setw(20)<<"Gross Income"<<left<<setw(15)<<"Taxes"<<left<<setw(10)<<"Net Income"<<endl;

input_data (fin, full_name, taxID, grossINCOME);
while (!fin.eof())
{

taxedAMOUNT = taxes(grossINCOME);
netINCOME = net_income(grossINCOME, taxedAMOUNT);
print_data (fout, full_name, taxID, grossINCOME, taxedAMOUNT, netINCOME);
input_data (fin, full_name, taxID, grossINCOME);
}
}


return 0;
}

Last edited on
Thanks for the update....

Ok so the problem is as I expect ... To make the problem skip over you need some kind of conditional statement to skip over the output operations. Right now you have:

1
2
3
4
5
6
7
8
9
			input_data (fin, full_name, taxID, grossINCOME);
			while (!fin.eof())
			{
				
				taxedAMOUNT = taxes(grossINCOME);
				netINCOME = net_income(grossINCOME, taxedAMOUNT);
				print_data (fout, full_name, taxID, grossINCOME, taxedAMOUNT, netINCOME);
				input_data (fin, full_name, taxID, grossINCOME);
			}


What you need is something like:


1
2
3
4
5
6
7
8
9
10
11
			input_data (fin, full_name, taxID, grossINCOME);
			while (!fin.eof())
			{
				if( this is a valid entry ) // <--- this is what you need
				{
					taxedAMOUNT = taxes(grossINCOME);
					netINCOME = net_income(grossINCOME, taxedAMOUNT);
					print_data (fout, full_name, taxID, grossINCOME, taxedAMOUNT, netINCOME);
				}
				input_data (fin, full_name, taxID, grossINCOME);
			}


I will leave it up to you to figure out what to put in the if statement.... now what you have in the input_data function is close but it only tests the tax_id you need to add the income consideration....:

if (!(tax_id >= 1000 && tax_id <= 9999))

The solution I think you are trying to get working:
-----------------------------------------------------------------

I suspect you are wondering why this if statement in the input data function does not skip over the data? the reason is you are testing inside one function but the output operation you want to skip over is outside that function. If you wanted the input function to perform the test, then you need to have that input function to tell the caller the result of that test (i.e. was the input is invalid?). Currently you have it returning void, you could change it to return true or false depending on whether the conditions are met. Then in the calling code you can save the result and use it in the if statement I added.

Now this is not really a good solution. In programming you should try to write functions that serves only one purpose. This way you can use the same code for other projects. This validity check is a separate purpose than simply reading a file. By putting it in the input function you can only use that input function in this program and no where else.

Improved solution:
-------------------------

Make the input program do input and only input. No testing nothing. Then based on the results of the input function, test whether the conditions are true just before the if statement I inserted for you. Now you could put the test logic (e.g. tax_id >= 1000 && tax_id <= 9999 && income>0 ) directly in the if statement but there is a better solution.

The test logic is something that you might want to place in a separate function that returns a bool. A separate function has benefits for code maintenance. lets say the gov changes the range of valid tax id's. Then you only need to change that one function. If you put that condition directly in the larger program, then you have to search the code and find all those times you performed the test. Also a separate function always means you can use the code for another program.

Last edited on
ahh ok i see how it creates a problem thank you
Topic archived. No new replies allowed.