// Lab 4 read and calculation file/input
// This program will read the data in the input file 
// used by grosspayMinustaxFileCode

#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

int main()
// this is the error message it displays if not working right to EXIT
{     
    ifstream inFile;
    ofstream outFile;
    int employee = 1;// I am trying to get loop initialized
    int exemptions, taxbracket; //the read other file cue
    double hours, overtime, payrate, grosspay, netpay, rate, tax;

    inFile.open("readGrosspayMinustax.cpp");
    if (inFile.fail())
    {
        cerr << "error opening input file.\n\n";
        system("pause");
        exit(1);
    }
     outFile.open("outputGrosspay");
     if (outFile.fail())
     {
        cerr << "error opening input file.\n\n";
        system("pause");
        exit(1);
     }

     outFile.setf(ios::fixed);

    inFile >> employee;
    inFile >> hours;
    inFile >> payrate; 
    inFile >> taxbracket;
    inFile >> exemptions;
    while (cin >> hours)
    {
       if (hours > 40)
       {
          overtime = hours - 40;
          hours = 40;
       }   
       else
          overtime = 0;
       grosspay = hours * payrate + overtime * payrate * 1.5;            
       if (taxbracket == 1)
          rate = .13;
       else if (taxbracket == 2)
          rate = .27;
       else
          rate = .35;

       tax = grosspay * (rate - .01 * exemptions);
       netpay = grosspay - tax;

    cout << fixed << showpoint << setprecision(2)<< endl;     
    cout << employee << hours <<  payrate <<  taxbracket <<  exemptions << endl;
    cout << grosspay << tax << netpay;

    inFile >> employee;
    inFile >> payrate; 
    inFile >> taxbracket;
    inFile >> exemptions;

    } 
    inFile.close();
    system("pause");
    return 0;
}
    \\ This is the read file where the fin.in file will derived data.

50.00 20.00 2 3
40.00 10.00 1 0
10.00 40.00 3 5
40.00 11.25 1 2

not getting any output right above this statement

Recommended Answers

All 20 Replies

while (cin >> hours)

I assume that you don't mean cin here, but rather inFile? Or does the user enter hours?

cout << fixed << showpoint << setprecision(2)<< endl;     
    cout << employee << hours <<  payrate <<  taxbracket <<  exemptions << endl;
    cout << grosspay << tax << netpay;

Same problem as before. You need to put tabs or spaces in between the different variables when you display them with the cout statement. Otherwise everything runs together and it's impossible to tell where one variable stops and the next one starts.

i am trying to understand my program

when i typed
while (cin >> hours) i can see it doesn't work

what i am trying to do is take what is in a file called inFile and read it so my program can use the information in that file

while (employee, hours, payrate, taxbracket, exemptions >> inFile)

my program is supposed to take the info in the file and do 2 things either calculate pay for 40 hours or calculate pay for 40 + overtime and output.

so question here
my while loop ... this is what i understand

while ( i am reading information in)
     {
          if (hours > 40) IF hours greater than 40 

     {
          overtime = hours - 40; CALCULATE total hours of overtime
          hours = 40;
          Then it would be out put from here
OTHERWISE

     }

else THERE IS NO OVERTIME and overtime will = 0
overtime = 0; overtime is now zero and will continue to calculate the regular 40 hours per week
grosspay = hours * payrate + overtime * payrate * 1.5;

if (taxbracket == 1)
rate = .13;
else if (taxbracket == 2)
rate = .27;
else
rate = .35;

tax = grosspay * (rate - .01 * exemptions);
netpay = grosspay - tax;
} here the while loop closes

cout << fixed '\t' << showpoint '\t' << setprecision(2) '\t' << endl; 
cout << employee '\t' << hours '\t' << payrate '\t' << taxbracket '\t' << exemptions'\t' << endl;
cout << grosspay '\t' << tax '\t' << netpay '\t' << endl;
}
{ 
inFile.close();
system("pause");
return 0;
} HOW DOES THIS LOOK?

i am trying to understand my program

when i typed
while (cin >> hours) i can see it doesn't work

what i am trying to do is take what is in a file called inFile and read it so my program can use the information in that file

while (employee, hours, payrate, taxbracket, exemptions >> inFile)


my program is supposed to take the info in the file and do 2 things either calculate pay for 40 hours or calculate pay for 40 + overtime and output.

so question here
my while loop ... this is what i understand


while ( i am reading information in)
{
if (hours > 40) IF hours greater than 40

{
overtime = hours - 40; CALCULATE total hours of overtime
hours = 40;
Then it would be out put from here
OTHERWISE

}

else THERE IS NO OVERTIME and overtime will = 0
overtime = 0; overtime is now zero and will continue to calculate the regular 40 hours per week
grosspay = hours * payrate + overtime * payrate * 1.5;

if (taxbracket == 1)
rate = .13;
else if (taxbracket == 2)
rate = .27;
else
rate = .35;

tax = grosspay * (rate - .01 * exemptions);
netpay = grosspay - tax;
} here the while loop closes

cout << fixed '\t' << showpoint '\t' << setprecision(2) '\t' << endl;
cout << employee '\t' << hours '\t' << payrate '\t' << taxbracket '\t' << exemptions'\t' << endl;
cout << grosspay '\t' << tax '\t' << netpay '\t' << endl;
}
{
inFile.close();
system("pause");
return 0;
} HOW DOES THIS LOOK?

Code tags and formatting please:

[code=cplusplus] // paste code here

[/code]

cout << fixed '\t' << showpoint '\t' << setprecision(2) '\t' << endl;
cout << employee '\t' << hours '\t' << payrate '\t' << taxbracket '\t' << exemptions'\t' << endl;
cout << grosspay '\t' << tax '\t' << netpay '\t' << endl;

This won't work. You need to put the << operator before '\t' like this:

cout << grosspay << '\t' << tax << '\t' << netpay << '\t' << endl;

I assume this is pseudocode and that you realize this won't work.

while (employee, hours, payrate, taxbracket, exemptions >> inFile)

The logic seems OK, but I can't comment much further till I see the actual updated code.

Download the original attachment

include <iostream>


#include <iomanip>


#include <fstream>


using namespace std;



int main()


// this is the error message it displays if not working right to EXIT


{


ifstream inFile;


ofstream outFile;


int employee = 1;                                                                    // I am trying to get loop initialized


int exemptions, taxbracket;                                                    //the read other file cue


double hours, overtime, payrate, grosspay, netpay, rate, tax;



inFile.open("readGrosspayMinustax.cpp");


if (inFile.fail())


{


cerr << "error opening input file.\n\n";


system("pause");


exit(1);


}


outFile.open("outputGrosspay");


if (outFile.fail())


{


cerr << "error opening input file.\n\n";


system("pause");


exit(1);


}


outFile.setf(ios::fixed);


while (hours, employee, payrate, taxbracket, expemptions >> inFile)


{


if (hours > 40)


{


overtime = hours - 40;


hours = 40;


}


else


overtime = 0;


grosspay = hours * payrate + overtime * payrate * 1.5;


if (taxbracket == 1)


rate = .13;


else if (taxbracket == 2)


rate = .27;


else


rate = .35;


tax = grosspay * (rate - .01 * exemptions);


netpay = grosspay - tax;



}


cout << fixed << '\t' << showpoint << '\t'<< setprecision(2)<< '\t' << endl;


cout << employee << '\t' << hours << '\t' << payrate << '\t' << taxbracket <<  '\t' <<exemptions << '\t' << endl;


cout << grosspay << '\t' << tax << '\t' << netpay << '\t' << endl;



{


inFile.close();


system("pause");


return 0;


}


*\\ This is the read file where the fin.in file will derived data.



50.00 20.00 2 3


40.00 10.00 1 0


10.00 40.00 3 5


40.00 11.25 1 2

I'm repeating VernonDozier here ...

Code tags and formatting please:

Just so that you get the file reading working, the following is almost completely wrong
(the use of placing the read operation inside the while() is good)

while (hours, employee, payrate, taxbracket, exemptions >> inFile)

Assuming you have five numbers on one line, you'll read those ...

while (inFile >> hours >> employee >> payrate >> taxbracket >> exemptions)
include <iostream>

#include <iomanip>

#include <fstream>

using namespace std; 


int main()

// this is the error message it displays if not working right to EXIT 

{ 

ifstream inFile;

ofstream outFile;

int employee = 1; // I am trying to get loop initialized

int exemptions, taxbracket; //the read other file cue

double hours, overtime, payrate, grosspay, netpay, rate, tax; 


inFile.open("readGrosspayMinustax.cpp");

if (inFile.fail())

{

cerr << "error opening input file.\n\n";

system("pause");

exit(1);

}

outFile.open("outputGrosspay");

if (outFile.fail())

{

cerr << "error opening input file.\n\n";

system("pause");

exit(1);

}

outFile.setf(ios::fixed);

while (inFile >> hours >> employee >> payrate >> taxbracket >> exemptions)

{

if (hours > 40)

{

overtime = hours - 40;

hours = 40;

} 

else

overtime = 0;

grosspay = hours * payrate + overtime * payrate * 1.5; 

if (taxbracket == 1)

rate = .13;

else if (taxbracket == 2)

rate = .27;

else

rate = .35;



tax = grosspay * (rate - .01 * exemptions);

netpay = grosspay - tax; 


}

cout << fixed << '\t' << showpoint << '\t'<< setprecision(2)<< '\t' << endl; 

cout << employee << '\t' << hours << '\t' << payrate << '\t' << taxbracket << '\t' <<exemptions << '\t' << endl;

cout << grosspay << '\t' << tax << '\t' << netpay << '\t' << endl;


{

inFile.close();

system("pause");

return 0;

}

*\\ This is the read file where the fin.in file will derived data.


50.00 20.00 2 3

40.00 10.00 1 0

10.00 40.00 3 5

40.00 11.25 1 2

The reading part still continued ...

// Following reads 5 numbers ...
while (inFile >> hours >> employee >> payrate >> taxbracket >> exemptions)

But the input lines contain only 4 numbers, so you need to drop the excess variable from the above code line. I guess the variable is employee , which appears to be a counter (note that you don't increment this counter anywhere).

Then you probably want to move those cout statements to be inside the while() loop.

Last but not least, in addition to the code tags, proper formatting makes the code for anyone much easier to read (and you'll probably get more replies).

Line 105 - that bracket looks out of place. Does this compile?

Line 57:

while (inFile >> hours >> employee >> payrate >> taxbracket >> exemptions)

I see you reading in five pieces of data for each trip through the while loop, yet your input file has four pieces of data per line:

50.00 20.00 2 3
40.00 10.00 1 0
10.00 40.00 3 5
40.00 11.25 1 2

These need to match. I do not see anything that represents "employee" in the data file, yet you are reading in "employee" in line 57.

The reading part still continued ...

// Following reads 5 numbers ...
while (inFile >> hours >> employee >> payrate >> taxbracket >> exemptions)

But the input lines contain only 4 numbers, so you need to drop the excess variable from the above code line. I guess the variable is employee , which appears to be a counter (note that you don't increment this counter anywhere).

Then you probably want to move those cout statements to be inside the while() loop.

Last but not least, in addition to the code tags, proper formatting makes the code for anyone much easier to read (and you'll probably get more replies).

Thanks.. i was confused for a time and deleted the counter for employee. Did I do the code tags correctly this last time?? i'll fix again and repost. thanks for the help. i need it.
mikelle

Did I do the code tags correctly this last time??
mikelle

Yes you did.
Only the formatting of code was lacking, e.g. proper indentation as below, works wonders ...

if (hours > 40)
{
    overtime = hours - 40;
    hours = 40;
}
else
{  // <- clearly state where the else block starts
    overtime = 0;
} // <- and where it ends
#include <iostream>

#include <iomanip>

#include <fstream>

using namespace std; 


int main()

// this is the error message it displays if not working right to EXIT 

{ 

ifstream inFile;

ofstream outFile;

int employee = 1; // I am trying to get loop initialized

int exemptions, taxbracket; //the read other file cue

double hours, overtime, payrate, grosspay, netpay, rate, tax; 


inFile.open("readGrosspayMinustax.cpp");

if (inFile.fail())

{

cerr << "error opening input file.\n\n";

system("pause");

exit(1);

}

outFile.open("outputGrosspay");

if (outFile.fail())

{

cerr << "error opening input file.\n\n";

system("pause");

exit(1);

}

outFile.setf(ios::fixed);

while (inFile >> hours >> payrate >> taxbracket >> exemptions)

{

if (hours > 40)

{

overtime = hours - 40;

hours = 40;

} 

else

overtime = 0;

grosspay = hours * payrate + overtime * payrate * 1.5; 

if (taxbracket == 1)

rate = .13;

else if (taxbracket == 2)

rate = .27;

else

rate = .35;



tax = grosspay * (rate - .01 * exemptions);

netpay = grosspay - tax; 
employees++;




cout << fixed << '\t' << showpoint << '\t'<< setprecision(2)<< '\t' << endl; 

cout << employee << '\t' << hours << '\t' << payrate << '\t' << taxbracket << '\t' <<exemptions << '\t' << endl;

cout << grosspay << '\t' << tax << '\t' << netpay << '\t' << endl;
}


{

inFile.close();

system("pause");

return 0;

}

*\\ This is the read file where the fin.in file will derived data.


50.00 20.00 2 3

40.00 10.00 1 0

10.00 40.00 3 5

40.00 11.25 1 2

I think it's time for you to discover the wonders of the tab key. Seriously, zero-width indentation is hard to read for everyone.

include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

int main()
                                                                                 // this is the error message it displays if not working right to EXIT 
{     
 	ifstream inFile;
 	ofstream outFile;
    	int employee = 1;                                                                    // I am trying to get loop initialized
    	int exemptions, taxbracket;                                                    //the read other file cue
    	double hours, overtime, payrate, grosspay, netpay, rate, tax;

    	inFile.open("readGrosspayMinustax.cpp");
    		if (inFile.fail())
    			{
       				cerr << "error opening input file.\n\n";
       				system("pause");
        				exit(1);
    			}
     	outFile.open("outputGrosspay");
    		 if (outFile.fail())
     			{
        				cerr << "error opening input file.\n\n";
        				system("pause");
        				exit(1);
     			}
     outFile.setf(ios::fixed);
    		 while (inFile >> hours >> payrate >> taxbracket >> exemptions)
    			{
       				if (hours > 40)
       				{
          					overtime = hours - 40;
         			 		hours = 40;
       				}   
       				else
          				{
overtime = 0;
   					grosspay = hours * payrate + overtime * payrate * 1.5;
           				}      
       				if (taxbracket == 1)
          				rate = .13;
       					else if (taxbracket == 2)
          					rate = .27;
       						else
          						rate = .35;
    tax = grosspay * (rate - .01 * exemptions);
    netpay = grosspay - tax;
    employee++;
    cout << fixed << '\t' << showpoint << '\t'<< setprecision(2)<< '\t' << endl;     
    cout << employee << '\t' << hours << '\t' << payrate << '\t' << taxbracket <<  '\t' <<exemptions << '\t' << endl;
    cout << grosspay << '\t' << tax << '\t' << netpay << '\t' << endl;

}

{
    inFile.close();
    system("pause");
    return 0;
}

*\\ This is the read file where the fin.in file will derived data. TRIED TABS HERE

50.00 20.00 2 3
40.00 10.00 1 0
10.00 40.00 3 5
40.00 11.25 1 2

That's a little too much on the tab key, but I appreciate the effort. A good IDE like Visual C++ actually has some good formatting options and will format for you with one click. It takes a little bit of configuration and practice (but not a whole lot), but it's worth learning. Your code will be much easier to read for both you and us. But again, thanks for making the effort to format.

So what's the question? You've posted the updated code. Does it work? What happens when you run it. What problems remain? What would you like us to do in response to this post?

That's a little too much on the tab key, but I appreciate the effort. A good IDE like Visual C++ actually has some good formatting options and will format for you with one click. It takes a little bit of configuration and practice (but not a whole lot), but it's worth learning. Your code will be much easier to read for both you and us. But again, thanks for making the effort to format.

So what's the question? You've posted the updated code. Does it work?

NO, UNFORTUNATELY IT DOES NOT AND THE PROBLEM IS WITH THE WHILE LOOP
I DON'T KNOW WHAT VARIABLE TO CALL IT
while (inFile >> hours >> payrate >> taxbracket >> exemptions) IS THIS JUST SUPPOSED TO ASSIGN THE STUFF IN THE FILE AND ASSIGN IT TO THE VARIABLES???

What happens when you run it.
WHEN I RUN IT IT JUST SAYS PRESS ANY KEY TO CONTINUE I TRIED AGAIN IT SAYS ERROR OPENING INPUT FILE QUESTION: HOW DO I SAVE THE FILE SO IT CAN BE OPENED?


What problems remain? What would you like us to do in response to this post?

HELP HELP HELP.. LOL I'M NOT GETTING IT.. I WONDER IF IT HAS TO DO WITH THE NAME OF THE FILE OR SOMETHING??

HELP HELP HELP.. LOL I'M NOT GETTING IT.. I WONDER IF IT HAS TO DO WITH THE NAME OF THE FILE OR SOMETHING??

Here is a commented and formatted version ...

#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

int main()
{     
    ifstream inFile;
    ofstream outFile;

    // Specify the[B] full path to the input file[/B],
    // in this case in the root of the C: drive.
    // If you specify the file name only, then the
    // file has to be in the program's current working
    // directory in order to be opened.
    inFile.open("c:\\readGrosspayMinustax.cpp");

    if (inFile.fail())
    {
        cerr << "error opening input file.\n\n";
        system("pause");
        exit(1);
    }
    
    outFile.open("outputGrosspay");

    if (outFile.fail())
    {
        cerr << "error opening input file.\n\n";
        system("pause");
        exit(1);
    }

    outFile.setf(ios::fixed);

    // [B]initialize all used variables[/B] ...
    int employee = 1;
    int exemptions = 0, 
        taxbracket = 0;

    double  hours = 0, 
            overtime = 0, 
            payrate = 0, 
            grosspay = 0, 
            netpay = 0, 
            rate = 0, 
            tax = 0;

    // read in the 4 numbers and assign to the variables
    while (inFile >> hours >> payrate >> taxbracket >> exemptions)
    {
        if (hours > 40)
        {
            overtime = hours - 40;
            hours = 40;
        }   
        else
        {
            overtime = 0;
            grosspay = hours * payrate + overtime * payrate * 1.5;
        }      
        
        if (taxbracket == 1)
            rate = .13;
        else if (taxbracket == 2)
            rate = .27;
        else
            rate = .35;

        tax = grosspay * (rate - .01 * exemptions);
        netpay = grosspay - tax;

        cout    << fixed 
                << showpoint 
                << setprecision(2)

                << employee   << '\t'
                << hours      << '\t'
                << payrate    << '\t'
                << taxbracket << '\t'
                << exemptions << '\t'
                << endl
                << grosspay   << '\t'
                << tax        << '\t'
                << netpay     << '\t'
                << endl;

        // next one ...
        employee++;
    }

    // { <- not needed
    inFile.close();
    system("pause");
    return 0;
    // } <- not needed
} // <- this one has been missing from your posts

When you post code, be sure to copy all the code, so that it will not be missing e.g. the closing curly brace.

Maybe you'll want to check the calculations and add code to write to the outFile too.

O.K., all of the advice given so far except for mitrmkar's last post regarding the path will not have anything to do with your problem since it all has to do with reading the file into variable name, displaying it, etc. Your problem is earlier. You can't open the file.

This line looks suspicious to me:

inFile.open("readGrosspayMinustax.cpp");

Is this the name of your input file? If not, that's your problem. It looks like possibly this the the name of the C++ program(before it's compiled), not a text input file. What is the name of the file that contains this?

50.00 20.00 2 3
40.00 10.00 1 0
10.00 40.00 3 5
40.00 11.25 1 2

If this file name is called "fin.in", as it appears to be:

*\\ This is the read file where the fin.in file will derived data. TRIED TABS HERE

you want to change this line:

inFile.open("readGrosspayMinustax.cpp");

to this:

inFile.open("fin.in");

Regarding the path, you don't need to put the path in the code if the input file is in the same directory as the executable program. If it is not in the same directory, you need to put the path, as mitrmkar mentioned. I would just make life easier on yourself and stick the input file in the same directory and don't bother with the path. If you specify the path and then later move the program and the input file, it won't work. That's not a problem if you leave off the path and put the input file and the program in the same directory.

//ALRIGHT I TRIED TO FIX AS MUCH AS I COULD AND GOT SOME OUTPUT...HOWEVER I'M NOT GETTING EMPLOYEE NUMBER 4'S INPUT. I NEED HELP WITH MY WHILE LOOP???

// Lab 4 read and calculation file/input
// This program will read the data in the input file
// used by grosspayMinustaxFileCode

#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

int main()
// this is the error message it displays if not working right to EXIT
{     
    int employee = 1;
    int exemptions, taxbracket;
    double hours, overtime, payrate, grosspay, netpay, rate, tax;

    ifstream inFile;
    inFile.open("readIn4.cpp");
    if (inFile.fail())
    {
        cerr << "error opening input file.\n\n";
        system("pause");
        exit(1);
    }
    ofstream outFile;
    outFile.open("outputGrosspay");
    if (outFile.fail())
    {
        cerr << "error opening input file.\n\n";
        system("pause");
        exit(1);
    }
     
  
    while (inFile >> employee)
    {
       inFile >> hours >> payrate >> taxbracket >> exemptions;  
       if (hours > 40)
       {
          overtime = hours - 40;
          hours = 40;
       }
       else
          overtime = 0;  
    grosspay = hours * payrate + overtime * payrate * 1.5;    
       if (taxbracket == 1)
          rate = .13;
       else if (taxbracket == 2)
          rate = .27;
       else
          rate = .35;
    tax = grosspay * (rate - .01 * exemptions);
    netpay = grosspay - tax;
    outFile.setf(ios::fixed);
    cout << fixed << showpoint << setprecision(2) << endl;     
    cout << employee << "\t\t" << hours << endl;
    cout << payrate <<    "\t\t" << taxbracket << endl;
    cout << exemptions <<  "\t\t" << grosspay << endl;
    cout << tax <<  "\t\t" << netpay << endl; 
    
    inFile >> employee >> hours >> payrate >> taxbracket >> exemptions;
    } 
    inFile.close();
    system("pause");
    return 0;
}

/*\\ This is the read file where the fin.in file will derived data.

1 50.00 20.00 2 3
2 40.00 10.00 1 0
3 10.00 40.00 3 5
4 40.00 11.25 1 2

output


1 40.00
20.00 2
3 1100.00
264.00 836.00

3 10.00
40.00 3
5 400.00
120.00 280.00
Press any key to continue . . .
Press any key to continue . . .*/

while (inFile >> employee)
    {
       inFile >> hours >> payrate >> taxbracket >> exemptions;  
       if (hours > 40)
       {
          overtime = hours - 40;
          hours = 40;
       }
       else
          overtime = 0;  
    grosspay = hours * payrate + overtime * payrate * 1.5;    
       if (taxbracket == 1)
          rate = .13;
       else if (taxbracket == 2)
          rate = .27;
       else
          rate = .35;
    tax = grosspay * (rate - .01 * exemptions);
    netpay = grosspay - tax;
    outFile.setf(ios::fixed);
    cout << fixed << showpoint << setprecision(2) << endl;     
    cout << employee << "\t\t" << hours << endl;
    cout << payrate <<    "\t\t" << taxbracket << endl;
    cout << exemptions <<  "\t\t" << grosspay << endl;
    cout << tax <<  "\t\t" << netpay << endl; 
    
    inFile >> employee >> hours >> payrate >> taxbracket >> exemptions;
    }

Please point out when you have changed the input file format in the future. I noticed that you were still reading in employee and started to write about that, but I now see that it is in the data file now, so that's fine.

You are missing more than the last employee. You are missing every other employee. Your input file has five pieces of data per line. You are reading ten pieces of data for every trip through the while loop in lines 1, 3, and 27. Get rid of line 27 altogether. You are reading in data in line 27, then immediately overwriting it in lines 1 and 3.

I'd suggest you to read a single whole line inside the while(), i.e.

while (inFile >> employee >> hours >> payrate >> taxbracket >> exemptions)
{
   ...

and then remove the line you have at the end of the while loop inFile >> employee >> hours >> payrate >> taxbracket >> exemptions;

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.