views:

250

answers:

3

I'm writing a program in Microsoft Visual Studio with C++ that will retrieve information from a .txt file. In that file there are negative numbers, but when I try to write a while loop that states what to do if there is a negative number, I get several errors.

Can someone please help me with this? Here is my code and I do realize there are errors but I can't figure out how to write the While loop statement to read these values which are hours worked and the hourly rate from the .txt file

Sample text file:

45.0    10.50
-35.0   7.75
50.0    12.00
45.0   -8.50
30.0    6.50
48.0    -10.25
-50.0   10.00
50.0    8.75
40.0    12.75
56.0    8.50

Code:

//*****************************
// This program is to help calculate an employee's weekly gross pay as well as
// the net pay while showing the taxes that were taken off.
// The data that will be shown will be calculated from a .txt file 
// that was created and called employee.txt.

// Input:  Will be the inFile known as employee.txt
// Output: Gross pay, taxable income, federal tax, state tax, and net pay

// Typed by:  
// Date:  
//******************************

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

using namespace std;

float computeGross(float, float);
void computeTaxes(float, float&, float&, float&);
float computeNetPay (float&, float&, float&, float&);

const float hours = 40;     // Regular 40 hour work week
const float ovTime = 1.5;      // Overtime if hours go over 40
const float exemption = 200.0;    // Exemption  if pay goes over 200
const float fedTaxRate = 0.10;    // Federal Tax Rate
const float stTaxRate = 0.03;     // State Tax rate

ifstream inFile;
ofstream outFile;

int main()
{
    inFile.open("employee.txt");
    outFile.open("result.txt");

    float hours, rate, grossPay, taxableIncome, fedTax, stTax, NetPay;
    inFile >> hours >> rate;

    while(inFile)
    {
        if {
            (hours <= 0)&& (rate <= 0);
            outFile << "Invalid Data";
        }
        else{ 
            return 0;
        }
    }

    grossPay = computeGross(hours, rate);
    computeTaxes (grossPay, taxableIncome, fedTax, stTax);
    computeNetPay (grossPay, fedTax, stTax, NetPay);

    outFile << fixed << showpoint << setprecision(2);
    outFile << "Hours worked = " << hours << endl 
            << "Hourly rate = " << rate  << endl     
            << "Employee's gross pay = " << grossPay << endl
            << "Taxable Income = " << taxableIncome << endl
            << "Federal Taxes = " << fedTax << endl
            << "State Taxes = " << stTax << endl
            << "Net Pay = " << NetPay << endl;
    return 0;
}

float computeGross (float h, float r)     //Computes for the Gross Pay
{
    if (h > hours) 
     return hours * r + (h - hours) * r * ovTime;
    else 
     return h * r;
}
void computeTaxes(float g, float& taxable, float& fedTax, float& stTax) //Computes both Taxes
{
    taxable = g - exemption;

    if (taxable > 0.0)
    {
      fedTax = fedTaxRate * taxable;
      stTax = stTaxRate * taxable;
    }
    else
    {
     fedTax = 0.0;
     stTax = 0.0;
    }
}

float computeNetPay (float& grossPay, float& fedTax, float& stTax, float& NetPay)
{
    return NetPay = grossPay - fedTax - stTax;    
}
+1  A: 

For a start, I think that this:

if {
    (hours <= 0)&& (rate <= 0);
    outFile << "Invalid Data";
    }

Should be this:

if ((hours <= 0) && (rate <= 0)) {
    outFile << "Invalid Data";
}

Note that to get code to format properly on StackOverflow, you should only use spaces, not tabs. I think that's whats causing your format issues.

Blorgbeard
ok I tried re-editing it so it looked better. Hopefully it does. Also when it runs, it doesn't retrieve all the information from the .txt file when it opens it. It only retrieves the first hour and hourly rate
+2  A: 

In your main function you have:

while(inFile)
{
    if ((hours <= 0) && (rate <= 0))
    {
        outFile << "Invalid Data";
    }
    else { 
        return 0;
    }
}

When the else is triggered the program finishes, the main function returns. You might want a continue break or nothing here instead, that return statement ends the main function not the While loop.

To get all the data out of the file your read statement ( inFile >> hours >> rate); will need to be in this or another loop. Say after the IF test for validity, it could be in the Else.

    while(inFile)
    {
         if ((hours <= 0) && (rate <= 0)) {

             outFile << "Invalid Data";
         }
         else { 
             // call the data functions
             // save the returned values     
         }

        //prime hours and rate for the next loop
        inFile >> hours >> rate;
    }
Paxic
Then he'd end up with a never-ending while loop, because the reading of the data does not happen inside the loop. That's something else in need of fixing.
Thomas
A continue? Could you please if you have time help me with this cause this program is for a class and my professor hasn't showed us how to use this function. I read the book, but doesn't have examples for this situation
You don't need anything at this point, the continue is just a way to make a loop go to the next iteration. If you put nothing in the else you should be fine.
Paxic
Good call on the continue, I'll strike that out!
Paxic
ok I'll give it a shot, thanks
@Thomas thanks for that heads up
Paxic
+2  A: 

Well.. my guess is this is what your looking for:

Note that the:

if ((hours <= 0) && (rate <= 0))

is changed to:

if ((hours <= 0) || (rate <= 0))

otherwise it won't ever hit the "invalid data" with your supplied data

//*****************************
// This program is to help calculate an employee's weekly gross pay as well as
// the net pay while showing the taxes that were taken off.
// The data that will be shown will be calculated from a .txt file 
// that was created and called employee.txt.

// Input:  Will be the inFile known as employee.txt
// Output: Gross pay, taxable income, federal tax, state tax, and net pay

// Typed by:  
// Date:  
//******************************

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

using namespace std;

float computeGross(float, float);
void computeTaxes(float, float&, float&, float&);
float computeNetPay (float&, float&, float&, float&);

const float hours = 40;        // Regular 40 hour work week
const float ovTime = 1.5;         // Overtime if hours go over 40
const float exemption = 200.0;       // Exemption  if pay goes over 200
const float fedTaxRate = 0.10;       // Federal Tax Rate
const float stTaxRate = 0.03;        // State Tax rate

int main()
{

    ifstream inFile ("employee.txt");
    ofstream outFile ("result.txt");

    float hours, rate, grossPay, taxableIncome, fedTax, stTax, NetPay;

    if (inFile.is_open())
    {
        while (! inFile.eof() )
        {

            inFile >> hours;
            inFile >> rate;

            if ((hours <= 0) || (rate <= 0))
            {
                outFile << "Invalid Data";
            }
            else
            { 
                grossPay = computeGross(hours, rate);
                computeTaxes (grossPay, taxableIncome, fedTax, stTax);
                computeNetPay (grossPay, fedTax, stTax, NetPay);

                outFile << fixed << showpoint << setprecision(2);
                outFile << "Hours worked = " << hours << endl   
                        << "Hourly rate = " << rate  << endl        
                        << "Employee's gross pay = " << grossPay << endl
                        << "Taxable Income = " << taxableIncome << endl
                        << "Federal Taxes = " << fedTax << endl
                        << "State Taxes = " << stTax << endl
                        << "Net Pay = " << NetPay << endl;
            }
        }
    }

    return 0;
}

The rest is the same

uzbones
Nice catch on the AND vs OR.
Paxic
ok I get one error now. Says thisError 1 fatal error C1083: Cannot open include file: 'stdafax.txt': No such file or directorywhen the file is saved in my Visual Studio folder with this project
Do you mean "stdafx.h" ? It is a generated file http://en.wikipedia.org/wiki/Precompiled_header.
Paxic
yeah sorry I did mean stdafax.h I'm not sure why it says this. Its the only error I have left
You should look for a Clean option in the build menu. Or try deleting the one file or or start a new project and add your hand coded classes to it...
Paxic
try http://stackoverflow.com/questions/221803/visual-studio-2008-clean-solution-option
Paxic
what do you mean by deleting the one file?
I have what's up above, I fixed my errors and I understand how he did that, but the stdafax.h is confusing me as to why it is above the description header
First do the Clean, then if that does not work remove the file that you say is there but it cannot find then Clean and Build
Paxic
If the include is not generated by your VS then comment it out of the code and see what happens
Paxic
I get several errors such as ifStream is an undeclared identifier, missing ";" before identifier "inFile", identifier "inFile" not found, "ofStream" undeclared identifier, missing ";" before identifier "outFile", "outFile" identifier not found, left of .is_open must have class/struct/union
and I also get the same with left of .eof must have class/struct/union and then it ends with unable to recover from previous errors, stopping compilation
I'm sorry if I'm being a pain but this is my first project like this and I was thrown into the deep end cause the professor never showed us how to do something like this
Sometimes you have to start from scratch. Start a new project. Add code a little at a time, add the includes as you need them. You want to debug one bug at a time.
Paxic
One last thing, I think there will be more io issues, you might need http://www.augustcouncil.com/~tgibson/tutorial/iotips.html - Good Luck
Paxic
This site is good for a tutorial if you don't understand something with c++http://www.cplusplus.com/doc/tutorial/files.htmlAnyway, sorry about the header above the comment, I missed cutting that out when I pasted it here...
uzbones
I do have to admit though that I compiled it as is above with VS2008... not VS2005 so it may be slightly different syntax if MS did something funny between versions...
uzbones