【问题标题】:Adding user input to a method and putting the results into a file (C++)将用户输入添加到方法并将结果放入文件 (C++)
【发布时间】:2015-03-08 18:12:54
【问题描述】:

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

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

【问题讨论】:

  • 您使用不同的文件名多次打开和重新打开输入文件流,但没有一次从文件中读取。

标签: c++ eclipse file input output


【解决方案1】:

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

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??? */

您的整个程序相当混乱。我对(主要)问题的最佳猜测是您根本没有向inFile 写任何东西。 outFile.open("examplee.txt") 之后的那 12 行似乎正在尝试完成以下任务:

inFile << id << ' ' << job_class << ' ' << years_service << ' ' << ED << '\n';

此外,虽然我猜这是出于调试目的,但您的许多方法什么都不做或未使用。例如,您使用emp.computation(job_class, years, ED),但之后您根本不使用emp。之后的三行似乎模仿了Employee::computation 和Employee::printout 的行为。

我建议你仔细考虑你要采取的具体步骤,然后想想像getline 和fstream::open 这样的方法的目的,并问自己“这是否完成了我的想法?”。因为当我阅读这段代码时,我真的很难理解你想要做什么。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-15
    • 2018-06-16
    • 2016-05-03
    • 1970-01-01
    • 1970-01-01
    • 2020-03-31
    • 2018-06-13
    • 1970-01-01
    相关资源
    最近更新 更多