在C ++中,为了在函数中使用数组,在其中使用ifstream将数据从.txt文件输入到数组中

麦可

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

#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;
}
阿德里安·哈梅林

原因很简单,您已将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。或返回一个数组。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

通过函数从数组到文件的C ++输入

使用C将输入与txt文件中的数据进行比较

C ++-从.txt文件存储到数组吗?

将文本文件输入到c中的2d数组中

C ++从txt文件中读取getline并存储到字符串数组中

我可以在C ++中使用ifstream数组吗

将输入文件存储到2D数组C ++中

将数组的指针传递给要在 C 中的 printf` 中使用的函数

从.txt文件读取到C中的数组

从txt文件读取变量并将其保存为数组C ++到类对象中

在其他c ++函数中使用c ++函数(在带有Rcpp的R Package中)

在 C 中使用数组(输出中的错误)

在C中的数组定义中使用宏

在 Visual Studio Code 中使用 C++ 读取 csv 文件并将数据存储在数组中

在 C 中使用带有数组的函数作为输入

在main函数(C)中使用数组

C中使用数组作为参数的函数

如何在C++中使用for循环将数组放入文件中

在C ++中使用ifstream逐行读取文件

文件到C中的动态数组

文件中的C ++数字到数组

在C中使用静态数组存储数据

将从 .txt 文件读取的字符串输入到数组中

我应该在将二维数组作为 C 中参数的函数的头中使用什么语法

如何将构造函数表达式放入数组中并在C#中使用它们?

从文件输入到类对象数组 c++

在Python中创建2D c_types数组并在C函数中使用它

从输入目录仅读取.txt文件,然后将所有内容getc放入C中的一个数组中

在C ++中使用冒号(':')访问数组中的元素(在Rcpp中)