Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何将输入到txt文件中的数据存储到数组中?_C++_Arrays_Function_Sorting - Fatal编程技术网

C++ 如何将输入到txt文件中的数据存储到数组中?

C++ 如何将输入到txt文件中的数据存储到数组中?,c++,arrays,function,sorting,C++,Arrays,Function,Sorting,我正在创建一个作为工资系统的程序,在该程序中我创建一个文件名并输入:ID(整数)、工资(双倍)、工时(整数)和总工资(双倍)。如何将输入的数据放入文件中,并将其放入数组中并显示。稍后,我将需要对它们进行排序,因为我在switch语句中有用于的案例 #include <fstream> #include <iostream> #include <iomanip> #include <string> using namespace std;

我正在创建一个作为工资系统的程序,在该程序中我创建一个文件名并输入:ID(整数)、工资(双倍)、工时(整数)和总工资(双倍)。如何将输入的数据放入文件中,并将其放入数组中并显示。稍后,我将需要对它们进行排序,因为我在switch语句中有用于的案例

#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>    

using namespace std;


//Function prototypes

void getEmployeeInfo(int&, double&, int&); //EmployeeInfo prototype

double calcWage(double, int, double&); //calcWage prototype

void printWages(ofstream &, int, double, int , double); //print wages prototype

//Main function

int main(){

    int ID, hours, caseInput;
    double pay, gross;
    char input;
    ofstream outputFile;
    string filename;

    do {

    cout << "              Menu             " << endl;

    cout << "1. Calculate gross wages for employees" << endl;

    cout << "2. Display employees information to screen" << endl;

    cout << "3. Display information in order of ID" << endl;

    cout << "4. Display information in order of hourly rate" << endl;

    cout << "5. Display information in order of hours worked" << endl;

    cout << "6. Display information in order of wage" << endl;

    cout << "7. Quit the system" << endl;

    cout << "Enter your option -->  ";

    cin >> caseInput;
        switch (caseInput) {
            case 1: //Create file

            cout << "Enter the filename: ";

            cin >> filename;

            //open file

            outputFile.open(filename.c_str());

            outputFile << setw(10) << "ID" << setw (15) << "Pay" << setw(17) << "Hours"

                    << setw(11) << "Gross"<<endl;

            outputFile << "---------------------------------------------------------\n";

            do{

            getEmployeeInfo(ID, pay, hours);

            calcWage(pay, hours, gross);

            printWages(outputFile, ID, pay, hours, gross);

            cout << "Do you want to enter another employee's information? ";

            cin >> input;

            }

            while(input == 'y' || input == 'Y');



            //If user does not enter y, close file

            outputFile.close();

            cout << "The result is reported to the file " << 

            filename << "." << endl;

            break;



        case 2: 

            break;



        case 3: 

            break;


        case 4: 

            break;

        case 5: cout << "Thank you for using Math Tutor." << endl;

            break;


        case 6: cout << "Thank you for using Math Tutor." << endl;

            break;


        case 7: cout << "Thank you for using the Payroll System." << endl;

            break;


        default: cout << "Error. Enter a number 1-7." << endl;

    }

        }

        while(caseInput != 7);


return 0;

}

//Function to input employee info

void getEmployeeInfo(int &ID, double &pay, int &hours){

cout << "Enter an employee's information by the order of ID number, rate, hours: ";

cin >> ID >> pay >> hours;

while(ID < 0){

cout << "You must enter a non negative value. Try again!" << endl;

cin >> ID;

}

while(pay < 0){

cout << "You must enter a non negative value. Try again!" << endl;

cin >> pay;

}


while(hours < 0){

cout << "You must enter a non negative value. Try again!" << endl;

cin >> hours;

}


}


//Function calculates gross pay

double calcWage(double pay, int hours, double &gross){

    gross = pay * hours;

    return gross;

}

//Print function

void printWages(ofstream &outputFile, int ID, double pay, int hours, double gross){

  outputFile << setw(10)<< ID << setw(12) << "$" << setprecision(2) 

  << fixed << pay << setw(13) << hours 

  << setw(10) << "$" << fixed << gross << endl;

}
#包括
#包括
#包括
#包括
使用名称空间std;
//功能原型
void getEmployeeInfo(int&,double&,int&)//EmployeeInfo原型
双计算年龄(双精度、整数、双精度和)//calcWage原型
无效打印工资(流和,整数,双精度,整数,双精度)//打印工资原型
//主要功能
int main(){
int ID、小时数、案例输入;
双薪,毛额;
字符输入;
流输出文件;
字符串文件名;
做{
cout
ifstream输入(“filename.txt”);
复制(istreambuf_迭代器(输入)、istreambuf_迭代器()、ostreambuf_迭代器(cout));
此代码将从txt文件中读取数据并将其打印到屏幕上。您可以参考以了解所有组件的工作方式


确保包含适当的标题。参考文件也会告诉您这一点。

我认为您可以将数据存储在一个新类中,然后首先将此数据保存在Vector中。如下所示:

// declare a class with all your employee info in.
class Info
{
public:
    Info(int id, int hours, double pay)
    {
        this.ID = id;
        this.hours = hours;
        this.pay = pay;
        this.gross = pay*hours;
    }

    int ID;
    int hours;
    double pay;
    double gross;
}

vector<Info> vInfo; // save your data from your input or file

// if you want to sort your data
std::sort(vInfo.begin(), vInfo.end(), [ ](Info &a, Info &b){
    // you can change this depend on your order
    // return a.xxx < b.xxxx
    return a.ID < b.ID;
});
// then use the printWagesVec to print the data in your case 2,3,4,5,6
// change print function
int printWagesVec(vector<Info>& vInfo)
{
    cout << setw(10) << "ID" << setw (15) << "Pay" << setw(17) << "Hours"
    << setw(11) << "Gross"<<endl;

    cout << "---------------------------------------------------------\n";

    vector<Info>::iterator it = vInfo.begin();
    for(; it != vInfo.end(); it++)
    {
        cout << setw(10)<< it->ID << setw(12) << "$" << setprecision(2) 
        << fixed << it->pay << setw(13) << it->hours 
        << setw(10) << "$" << fixed << it->gross << endl;
    }

    return 0;
}
//声明一个包含所有员工信息的类。
班级信息
{
公众:
信息(整数id,整数小时,双倍支付)
{
this.ID=ID;
这个.小时=小时;
这个。支付=支付;
this.gross=工资*小时;
}
int-ID;
整小时;
双薪;
双倍毛;
}
vector vInfo;//保存输入或文件中的数据
//如果要对数据进行排序
排序(vInfo.begin(),vInfo.end(),[])(信息与a,信息与b){
//您可以根据您的订单更改此选项
//返回a.xxx首先,您需要声明一个数组或数据结构(我建议
std::vector
)来存储数据。
// declare a class with all your employee info in.
class Info
{
public:
    Info(int id, int hours, double pay)
    {
        this.ID = id;
        this.hours = hours;
        this.pay = pay;
        this.gross = pay*hours;
    }

    int ID;
    int hours;
    double pay;
    double gross;
}

vector<Info> vInfo; // save your data from your input or file

// if you want to sort your data
std::sort(vInfo.begin(), vInfo.end(), [ ](Info &a, Info &b){
    // you can change this depend on your order
    // return a.xxx < b.xxxx
    return a.ID < b.ID;
});
// then use the printWagesVec to print the data in your case 2,3,4,5,6
// change print function
int printWagesVec(vector<Info>& vInfo)
{
    cout << setw(10) << "ID" << setw (15) << "Pay" << setw(17) << "Hours"
    << setw(11) << "Gross"<<endl;

    cout << "---------------------------------------------------------\n";

    vector<Info>::iterator it = vInfo.begin();
    for(; it != vInfo.end(); it++)
    {
        cout << setw(10)<< it->ID << setw(12) << "$" << setprecision(2) 
        << fixed << it->pay << setw(13) << it->hours 
        << setw(10) << "$" << fixed << it->gross << endl;
    }

    return 0;
}