Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.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++ 向方法添加用户输入并将结果放入文件(C+;+;)_C++_Eclipse_File_Input_Output - Fatal编程技术网

C++ 向方法添加用户输入并将结果放入文件(C+;+;)

C++ 向方法添加用户输入并将结果放入文件(C+;+;),c++,eclipse,file,input,output,C++,Eclipse,File,Input,Output,任何帮助都将不胜感激。我所要做的就是请求用户输入,做一些计算并将结果打印到文件中。我认为我的代码是正确的,但当我运行我的程序时,我一无所获。这是我的密码。不是在寻找答案,只是想得到一些提示来引导我走向正确的方向。谢谢 #include<iostream> #include<fstream> #include<string> using namespace std; class Employee{ private: int id; int jo

任何帮助都将不胜感激。我所要做的就是请求用户输入,做一些计算并将结果打印到文件中。我认为我的代码是正确的,但当我运行我的程序时,我一无所获。这是我的密码。不是在寻找答案,只是想得到一些提示来引导我走向正确的方向。谢谢

#include<iostream>
#include<fstream>
#include<string>
using namespace std;

class Employee{
private:
    int id;
    int job_class;
    int years_service;
    int Ed;
    float salary;
public:
    void getData(ifstream&);
    void computation(int job_class, int years_service, int Ed);
    void printout(ofstream&);
};

void Employee::getData(ifstream& infile){

infile >> id >> job_class >> years_service >> Ed;

}

void Employee::computation(int job_class, int years_service, int Ed){
int basePay = 800;
float jobresult, Yearresult, Edresult;

if(job_class == 1){
 jobresult = .05;
}

if(job_class == 2){
 jobresult = .10;
}

if(job_class == 3){
 jobresult = .15;
}

if(years_service <= 10){
 Yearresult =.05;
}

if(years_service > 10){
 Yearresult = .05;
}

if(Ed == 1){
 Edresult = .00;
}

if(Ed == 2){
 Edresult = .05;
}

if(Ed == 3){
 Edresult = .12;
}

if(Ed == 4){
 Edresult = .20;
}
salary = basePay + jobresult + Yearresult + Edresult;
//cout << salary;
}

void Employee::printout(ofstream& outfile){
outfile << "ID: " << "Salary " << endl;
outfile << id << salary;
}

int main(){

Employee emp; //created an Employee object
string input;


int id;
int job_class;
int years_service;
int Ed;
int basepay = 800;

cout << "Enter id" << endl;
cin >> id;
cout << "Enter job_class" << endl;
cin >> job_class;
cout << "Enter years of service" << endl;
cin >> years_service;
cout << "Enter education" << endl;
cin >> Ed;


 ifstream inFile;
 ofstream outFile;

//getline(cin, input);



 inFile.open("example.txt");
 outFile.open("examplee.txt");

//inFile.open(input);

std::string r = std::to_string(id); //converted id to string
inFile.open(r);
getline(cin, r);

std::string s = std::to_string(years_service);
inFile.open(s);
getline(cin, s);


std::string t = std::to_string(years_service);
inFile.open(t);
getline(cin, t);

 std::string u = std::to_string(Ed);
 inFile.open(u);
getline(cin, u);

if(inFile.is_open()){

emp.getData(inFile);
inFile.close();
}

outFile.open(r);

if(outFile.is_open()){

emp.computation(job_class, years_service, Ed);
float sal = basepay + job_class + years_service + Ed;

outFile << "ID " << "Salary " << endl;
outFile << id << sal;

outFile.close();
return 0;
}
}
#包括
#包括
#包括
使用名称空间std;
班级员工{
私人:
int-id;
国际职业培训班;
国际服务年;
int-Ed;
浮动工资;
公众:
void getData(ifstream&);
无效计算(国际工作等级、国际工作年限、国际教育);
无效打印输出(流和);
};
void Employee::getData(ifstream和infle){
填写>>身份>>工作等级>>服务年限>>教育;
}
void Employee::计算(int job_class,int years_service,int Ed){
国际基本工资=800;
浮动作业结果、年度结果、Edresult;
如果(作业类==1){
jobresult=.05;
}
如果(作业类==2){
jobresult=.10;
}
如果(作业类==3){
jobresult=.15;
}
if(服务年限10年){
年结果=.05;
}
如果(Ed==1){
Edresult=.00;
}
如果(Ed==2){
Edresult=.05;
}
如果(Ed==3){
Edresult=.12;
}
如果(Ed==4){
Edresult=.20;
}
工资=基本工资+工作结果+年度结果+Edresult;

//你到底想对这样的事情做些什么

std::string r = std::to_string(id); //converted id to string
inFile.open(r);   /*Opens a file whose name is <id> ???*/
getline(cin, r);  /*Overwrites the contents of r and does nothing??? */
另外,虽然我猜这是为了调试目的,但您的许多方法都不做任何事情或未使用
但在这之后,您根本不使用
emp
。之后的三行似乎模仿了
Employee::computation
Employee::printout
的行为


<>我建议你仔细考虑你要采取的具体步骤,然后想想方法的目的,比如“代码> GETLION<代码>和<代码> fSt::打开< /Cord>,问问你自己:“这是否完成了我的任务?”。因为当我阅读此代码时,我真的很难理解您试图做什么。

您使用不同的文件名多次打开并重新打开输入文件流,但没有一次从文件中读取。
inFile << id << ' ' << job_class << ' ' << years_service << ' ' << ED << '\n';