0

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;    
}
Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
  • Sorry, it doesn't look exactly like that...didn't space everything correctly. It's just I can't figure out how to get the While loop to read the negative numbers too –  Feb 25 '09 at 02:57
  • Could you please fix the formatting and provide a sample output? – David Segonds Feb 25 '09 at 03:01
  • Sorry, I think I fixed it. when I run the code I only get this output Hours worked = 45.00 Hourly rate = 10.50 Employee's gross pay = 498.75 Taxable Income = 298.75 Federal Taxes = 29.88 State Taxes = 8.96 Net Pay = 459.91 –  Feb 25 '09 at 03:17

3 Answers3

2

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
  • 1,760
  • 16
  • 28
  • 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 Feb 25 '09 at 03:23
  • 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 –  Feb 25 '09 at 03:24
  • 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 Feb 25 '09 at 03:26
  • Good call on the continue, I'll strike that out! – Paxic Feb 25 '09 at 03:27
2

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
  • 1,422
  • 11
  • 15
  • ok I get one error now. Says this Error 1 fatal error C1083: Cannot open include file: 'stdafax.txt': No such file or directory when the file is saved in my Visual Studio folder with this project –  Feb 25 '09 at 04:50
  • Do you mean "stdafx.h" ? It is a generated file http://en.wikipedia.org/wiki/Precompiled_header. – Paxic Feb 25 '09 at 05:16
  • yeah sorry I did mean stdafax.h I'm not sure why it says this. Its the only error I have left –  Feb 25 '09 at 05:18
  • 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 Feb 25 '09 at 05:23
  • try http://stackoverflow.com/questions/221803/visual-studio-2008-clean-solution-option – Paxic Feb 25 '09 at 05:25
  • what do you mean by deleting the one file? –  Feb 25 '09 at 05:29
  • 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 –  Feb 25 '09 at 05:38
  • 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 Feb 25 '09 at 05:39
  • If the include is not generated by your VS then comment it out of the code and see what happens – Paxic Feb 25 '09 at 05:41
  • 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 –  Feb 25 '09 at 05:44
  • 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 –  Feb 25 '09 at 05:45
  • 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 –  Feb 25 '09 at 05:46
  • 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 Feb 25 '09 at 05:46
  • 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 Feb 25 '09 at 06:32
  • This site is good for a tutorial if you don't understand something with c++ http://www.cplusplus.com/doc/tutorial/files.html Anyway, sorry about the header above the comment, I missed cutting that out when I pasted it here... – uzbones Feb 25 '09 at 11:59
  • 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 Feb 25 '09 at 12:01
1

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
  • 101,031
  • 48
  • 228
  • 272
  • 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 –  Feb 25 '09 at 03:14