Friday, April 8, 2011

How do I fix my while Error?

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;    
}
From stackoverflow
  • 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.

  • 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;
        }
    
    Thomas : 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.
    Paxic : 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 : @Thomas thanks for that heads up
  • 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

    Paxic : Nice catch on the AND vs OR.
    Paxic : Do you mean "stdafx.h" ? It is a generated file http://en.wikipedia.org/wiki/Precompiled_header.
    Paxic : 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 : 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 : 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
    uzbones : 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 : 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...

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.