2015-03-08 44 views
0

任何幫助,將不勝感激。我想要做的是要求用戶輸入,做一些計算並將結果打印在一個文件中。我認爲我的代碼是正確的,但是當我運行我的程序時,我什麼也得不到。這是我的代碼。不尋找答案,只是爲了讓我的方向正確。謝謝。將用戶輸入添加到方法並將結果放入文件(C++)

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

您可以用不同的文件名多次打開並重新打開輸入文件流,而不是從文件中讀取這些次。 – 2015-03-08 18:17:38

回答

0

你到底在做什麼?

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

您的整個程序相當混亂。關於(主要)問題,我最好的猜測是你根本沒有寫任何東西給​​。 outFile.open("examplee.txt")後的12條線路看起來他們正在努力實現以下目標:

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

而且,雖然我猜這是爲了調試的目的,但很多的你的方法什麼都不做或不使用。例如,您使用emp.computation(job_class, years, ED),但在此之後,您完全不以任何方式使用emp。之後的三行似乎模仿了Employee::computationEmployee::printout的行爲。

我建議你仔細考慮你正在嘗試採取的具體步驟,然後考慮諸如getlinefstream::open等方法的目的,並問自己:「這是否完成了我所想要的任務?」。因爲當我閱讀這些代碼時,我非常努力地理解你想要做什麼。

相關問題