【问题标题】:In C++ for the use of arrays within functions, where data is entered into arrays from a .txt file using ifstream在 C++ 中用于在函数中使用数组,其中使用 ifstream 将数据从 .txt 文件输入到数组中
【发布时间】:2015-11-17 12:26:31
【问题描述】:

第一次在这里发帖。该问题要求使用函数计算并输出包含总工资、净工资等的工资单并将条件输出到文本文件中。但是我的问题是当我使用数组时函数拒绝循环。

#include <iostream>
#include <fstream>//File stream
#include <string> //For use with string variables
#include <iomanip>//For use with formatting

double hoursworked[6],hourlyrate[6];
int i;

using namespace std;


double gross_fun(double hourlyrate[], double hoursworked[],int &numofelements) 

{
    double overtime[6];
    double gross[6];

    for(i=0; i<=5; i++){
        if (hoursworked[i] > 40){ 
                overtime[i] = 1.5*hourlyrate[i] * (hoursworked[i]-40); //Overtime is 1.5 times hourly rate
                    gross[i]=overtime[i]+(40*hourlyrate[i]);

            }

    else
        gross[i]= hourlyrate[i]  * hoursworked[i];


return gross[i];
}
}

double taxes_fun(double gross[], int &numofelements) 
{
    double taxes;

    for(i=0; i<=5; i++){

        taxes = .1 * gross[i]; 

    return taxes;
}
}

double SS_fun( double gross[], int &numofelements) 
    {
        double social;


        for(i=0; i<=5; i++){
            social = .05 * gross[i]; 
        return social;
    }
}

double netpay_fun(double gross[], double tax[], double social[], int &numofelements) 
    {
        double netpay;

        for(i=0; i<=5; i++){
        netpay= gross[i] - (tax[i] + social[i]); //Resultant net pay given when taxes and security are subtracted
        return netpay;
        }
    }


double taxes(double tax[], int &numofelements);
double social_security (double social[], int &numofelements);
double netpay_fun  (double netpay[], int &numofelements);
double gross_fun(double hourlyrate[], double hoursworked[], int &numofelements);

int main () 
{
    ifstream inFile; //To read .txt file
    inFile.open ("input.txt");

        if (inFile.fail()){ 
        cerr << "Error Opening File" << endl;
        exit(1);
    }

    int EmpNum[6];//Employee Number
    int i = 0;    //Counter
    double gross[6],taxes[6],socia[6],netpay[6],moneytopay[6];
    double overtime[6],hoursworked[6],hourlyrate[6],social[6],totalnetpay = 0.0f;
    char paytype[6];
    string firstname[6],lastname[6];
    cout << setprecision(2) << fixed; //Set double values to two decimal places

    (inFile>>EmpNum[i]>>firstname[i]>>lastname[i]>>hoursworked[i]>>hourlyrate[i]>>paytype[i]){
        i++;
    }

    for(i=0;i<=5;i++){
        gross[i]=gross_fun(hourlyrate, hoursworked, i); 
        taxes[i] = taxes_fun(gross, i);
        social[i] = SS_fun (gross, i);
        netpay[i] = netpay_fun  (gross, taxes, social, i);
        totalnetpay=totalnetpay+netpay[i];      


        }
    }


    cout << setw(6)  << "EmpNo"<< setw(13) << "First Name"<< setw(13)  << "Last Name"<< setw(8)  << "Gross";
    cout << setw(8)  << "Tax"<< setw(8)  << "SS"<< setw(10)  << "Net Pay"<< setw(9)  << " Payment Type";

    for(i=0;i<6;i++){

        cout << setw(5)  <<EmpNum[i]<< setw(11) <<firstname[i]<<setw(14)  <<lastname[i]<< setw(11)  << gross[i]<< setw(8)  << taxes[i];
        cout << setw(9)  << social[i]<< setw(8)  << netpay[i]<< setw(7)  << paytype[i]<< endl;
    }

    cout<<"\nSum of netpay: "<<totalnetpay;
    return 0;
}

【问题讨论】:

  • 请出示MCVE
  • 您是否收到错误消息?
  • 否,但它只使用计算中的第一行输入。 link这是文本输入文件的屏幕link
  • 您的函数不是“拒绝循环”,但您在循环体内有 return。因此,您在第一次执行循环后返回。
  • @Michael 正确格式化您的代码对您自己和他人都有帮助。

标签: c++ arrays string function fstream


【解决方案1】:

原因很简单,您将return 语句放在for 循环中。把它移出来。

double gross_fun(double hourlyrate[], double hoursworked[],int &numofelements) //Function to determine gross salary

{
    double overtime[6];
    double gross[6];
    double total = 0.0;

    for(i=0; i<=5; i++){
        if (hoursworked[i] > 40){ //Calculating gross salary if hours worked greater than 40
                overtime[i] = 1.5*hourlyrate[i] * (hoursworked[i]-40); //Overtime is 1.5 times hourly rate
                    gross[i]=overtime[i]+(40*hourlyrate[i]);

            }

    else
        gross[i]= hourlyrate[i]  * hoursworked[i]; //Calculating gross salary if no overtime is done

// remove here
}
return // something
}

您在所有函数中都做了同样的事情。如果你想返回一个数组,我建议你在参数中传递它并返回 void。或者返回一个数组。

【讨论】:

  • 我所有的值都是 0,你能解释一下传递参数并返回 void 的意思吗?
  • 您在函数的参数列表中添加一个参数,该参数仅用于写入,不能读取。就你而言,类似的东西:double gross_fun(double hourlyrate[], double hoursworked[], double gross[])。你返回的地方是你什么都不写,你已经用一个参数替换了你的局部变量gross
  • 非常感谢,您能告诉我为什么它输出的第一行或最后一行计算不正确。对于 i=0;i
  • 我不能肯定地说,但我在您的代码中发现了另外两个问题。第一个是在main() 中设置精度后,您只读取一行。你不是忘记(inFile&gt;&gt;...之前的while键了吗?另外,你的文件大小是多少?有时你放 5,有时 6,你应该定义一个 const unsigned int SIZE = 6 或其他东西,然后只使用那个变量。暂时不能多说。
猜你喜欢
  • 2019-02-27
  • 1970-01-01
  • 1970-01-01
  • 2021-03-29
  • 2019-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多