如何修复我的错误?

时间:2009-02-25 02:53:03

标签: c++ visual-studio-2005

我正在使用C ++在Microsoft Visual Studio中编写一个程序,该程序将从.txt文件中检索信息。在该文件中有负数,但是当我尝试编写一个while循环,说明如果有负数时该怎么做,我会得到几个错误。

有人可以帮我这个吗?这是我的代码,我确实意识到有错误,但我无法弄清楚如何编写While循环语句来读取这些工作小时数和.txt文件中的小时费用

示例文本文件:

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

代码:

//*****************************
// 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;    
}

3 个答案:

答案 0 :(得分:2)

在您的主要功能中,您有:

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

当触发else时程序结束,main函数返回。你可能想要一个 continue 中断或者没有任何内容,返回语句结束主函数而不是While循环。

从文件中获取所有数据的读取语句(inFile >> hours >> rate); 将需要在这个或另一个循环中。在IF测试有效性之后说,它可能在其他地方。

    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;
    }

答案 1 :(得分:2)

嗯..我的猜测是你正在寻找的东西:

请注意:

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

更改为:

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

否则它将不会使用您提供的数据点击“无效数据”

//*****************************
// 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;
}

其余的是相同的

答案 2 :(得分:1)

首先,我认为:

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

应该是这样的:

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

请注意,要使代码在StackOverflow上正确格式化,您应该只使用空格,而不是制表符。我认为这是导致格式问题的原因。