2017-11-09 11 views
-2
#include<iostream> 
#include<string> 
#include<fstream> 
#include<vector> 


using namespace std; 
void check(ifstream &iFile) 
{ 
    if (!iFile.is_open()) 
    { 
     cout << "Data file not found!" << endl; 
     system("pause"); 
     exit(1); // exit the program if the file is not found. 
    } 
} 

void readIn(ifstream &iFile, vector<string> &fName, vector<string> &lName, vector<string> &jTitle, vector<string> &eID, vector<double> &hoursWorked, vector<double> &wage, vector<int> &deductions, vector<double> &sPay, string sTemp, double dTemp, int iTemp) 
{ 
    while (!iFile.eof()) 
    { 
     iFile >> sTemp; 
     fName.push_back(sTemp); 

     iFile >> sTemp; 
     lName.push_back(sTemp); 

     iFile.ignore(); 
     getline(iFile, sTemp); 
     jTitle.push_back(sTemp); 

     iFile >> sTemp; 
     eID.push_back(sTemp); 

     iFile >> dTemp; 
     hoursWorked.push_back(dTemp); 

     iFile >> dTemp; 
     wage.push_back(dTemp); 

     iFile >> iTemp; 
     deductions.push_back(iTemp); 

     iFile >> dTemp; 
     sPay.push_back(dTemp); 

    } 
    cout << "completed" << endl; 

} 

int main() 
{ 
    ifstream iFile; 
    iFile.open("data.txt"); 
    check(iFile); 

    vector<string> fName, lName, eID, eStatus, jTitle; 
    vector<double> nPay, gPay, oPay, oHours; 
    vector<double> hoursWorked, wage, sPay; 
    vector<int> deductions; 

    // temporary names to pass to the vector 
    string sTemp; // string temp 
    double dTemp=0; // double temp 
    int iTemp=0; // integar temp 
    readIn(iFile, fName, lName, jTitle, eID, hoursWorked, wage, deductions, sPay, sTemp, dTemp, iTemp); 
/* while (!iFile.eof()) 
    { 
     iFile >> sTemp; 
     fName.push_back(sTemp); 

     iFile >> sTemp; 
     lName.push_back(sTemp); 

     iFile.ignore(); 
     getline(iFile, sTemp); 
     jTitle.push_back(sTemp); 

     iFile >> sTemp; 
     eID.push_back(sTemp); 

     iFile >> dTemp; 
     hoursWorked.push_back(dTemp); 

     iFile >> dTemp; 
     wage.push_back(dTemp); 

     iFile >> iTemp; 
     deductions.push_back(iTemp); 

     iFile >> dTemp; 
     sPay.push_back(dTemp); 
    }*/ 

    int sizeOf = fName.size(); 
    for (int a = 0; a < sizeOf; a++) 
    { 
     cout << fName.size() << " FName " << fName[a] << " LName " << lName[a] << " JobTitle " << jTitle[a] << endl; 
     cout << "EmployeeID " << eID[a] << " Hours Worked " << hoursWorked[a] << " Hourly Wage " << wage[a] << endl; 
     cout << "Deductions " << deductions[a] << " Salary Pay " << sPay[a] << endl; 
    } 

    system("pause"); 
    return 0; 
} 

私の機能は何もしません。それはコンパイルされますが、出力はありません。事は、それが完全にうまく動作するすべての部分からベクトルsPayを取り出すときです。その部分がなぜ機能していないのかについての示唆はありますか?私の限られた知識から、それは完全にうまくいくはずですが、私はこれを引き起こす原因を理解できません。ベクトル関数プログラムの問題

私の例のテキストファイルには、あなたの入力ファイルはあなたの読書コードが一致しません

Alan 
WakeField 
IT GUY 
T2034 
40 
15 
1 
Hourly 
0.00 
+0

[なぜループ状態のiostream :: eofが間違っていると思われますか?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered -wrong) –

+0

私はあなたに[良い初心者の本を拾う]ことをお勧めします。(http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) *および*クラス*を使用して密接に関連するデータをグループ化します。もちろん、ローカル変数についても! –

+0

自分自身に質問してください: 'sPay'を読んでもプログラムが動作しない場合は、入力に何か問題があるはずです。その前に何を読んでいますか、入力ファイルの次のデータは何ですか? – 1201ProgramAlarm

答えて

1

です。ファイルには9つの値が表示されていますが、コードでは8つの値しか読み取ろうとしていません。 readIn()は、このコードになると

iFile >> dTemp; 
sPay.push_back(dTemp); 

それはdoubleを読み取ろうとするが、ファイルではなくHourlyを持っているので、読み取りに失敗しました。

したがって、Hourly行をファイルから削除するか、iFile >> sTempにその行を読み込むように呼び出します。

また、パラメータstring sTemp,double dTemp、およびint iTempは、入力パラメータではなくローカル変数として宣言する必要があります。

また、readIn()はエラー処理は行っていません。 main()コードでは、ファーストネームのベクトルが他のベクトルのサイズと正確に一致するという無効な前提がありますが、readIn()はそれを保証しません。

最後に、何かを読む前にeof()をチェックするのは間違っています。ストリームのeofbitフラグは、読み取り操作が過去のEOFを読み取ろうとするまで更新されません。

このコードを書き直すことを検討する必要があります。

また
#include <iostream> 
#include <string> 
#include <fstream> 
#include <vector> 

struct Employee 
{ 
    std::string fName; 
    std::string lName; 
    std::string title; 
    std::string eID; 
    double hoursWorked; 
    double wage; 
    int deductions; 
    std::string wageType; 
    double sPay; 

    Employee() : 
     hoursWorked(0), wage(0), deductions(0), sPay(0) 
    { 
    } 
}; 

void check(std::ifstream &iFile) 
{ 
    if (!iFile.is_open()) 
    { 
     std::cout << "Data file not found or unable to open!" << std::endl; 
     std::system("pause"); 
     exit(1); // exit the program. 
    } 
} 

void readIn(std::ifstream &iFile, std::vector<Employee> &employees) 
{ 
    std::ios_base::iostate oldstate = iFile.exceptions(); 
    iFile.exceptions(std::ifstream::badbit | std::ifstream::failbit); 

    try 
    { 
     do 
     { 
      Employee emp; 

      iFile >> emp.fName; 
      iFile >> emp.lName; 
      std::getline(iFile, emp.title); 
      iFile >> emp.eID; 
      iFile >> emp.hoursWorked; 
      iFile >> emp.wage; 
      iFile >> emp.deductions; 
      iFile >> emp.wageType; 
      iFile >> emp.sPay; 

      employees.push_back(emp); 
     } 
     while (!iFile.eof()); 
    } 
    catch (const std::ios_base::failure &) 
    { 
     std::cout << "Data file corrupted!" << std::endl; 
     std::system("pause"); 
     exit(1); // exit the program. 
    } 

    iFile.exceptions(oldstate); 

    std::cout << "completed" << std::endl; 
} 

int main() 
{ 
    std::ifstream iFile("data.txt"); 
    check(iFile); 

    std::vector<Employee> employees; 

    readIn(iFile, employees); 

    int sizeOf = employees.size(); 
    for (int a = 0; a < sizeOf; a++) 
    { 
     std::cout << "FName " << employees[a].fName 
        << " LName " << employees[a].lName 
        << " JobTitle " << employees[a].title 
        << std::endl; 
     std::cout << "EmployeeID " << employees[a].eID 
        << " Hours Worked " << employees[a].hoursWorked 
        << " << employees[a].wageType << " Wage " << employees[a].wage 
        << std::endl; 
     std::cout << "Deductions " << employees[a].deductions 
        << " Salary Pay " << employees[a].sPay 
        << std::endl; 
     std::cout << std::endl; 
    } 

    std::system("pause"); 
    return 0; 
} 

、あなたのデータはラインベースであるため、あなたはそれぞれの行を読み取るためにstd::getline()を使用して、値を解析するstd::istringstreamを使用する必要があります:

void readIn(std::ifstream &iFile, std::vector<Employee> &employees) 
{ 
    std::string sTemp; 

    std::ios_base::iostate oldstate = iFile.exceptions(); 
    iFile.exceptions(std::ifstream::badbit | std::ifstream::failbit); 

    try 
    { 
     do 
     { 
      Employee emp; 

      std::getline(iFile, emp.fName); 
      std::getline(iFile, emp.lName); 
      std::getline(iFile, emp.title); 
      std::getline(iFile, emp.eID); 

      std::getline(iFile, sTemp); 
      if (!(std::istringstream(sTemp) >> emp.hoursWorked)) 
       iFile.setstate(std::ifstream::failbit); 

      std::getline(iFile, sTemp); 
      if (!(std::istringstream(sTemp) >> emp.wage)) 
       iFile.setstate(std::ifstream::failbit); 

      std::getline(iFile, sTemp); 
      if (!(std::istringstream(sTemp) >> emp.deductions)) 
       iFile.setstate(std::ifstream::failbit); 

      std::getline(iFile, emp.wageType); 

      std::getline(iFile, sTemp); 
      if (!(std::istringstream(sTemp) >> emp.sPay)) 
       iFile.setstate(std::ifstream::failbit); 

      employees.push_back(emp); 
     } 
     while (!iFile.eof()); 
    } 
    catch (const std::ios_base::failure &) 
    { 
     std::cout << "Data file corrupted!" << std::endl; 
     std::system("pause"); 
     exit(1); // exit the program if the file is corrupted. 
    } 

    iFile.exceptions(oldstate); 

    std::cout << "completed" << std::endl; 
} 
+0

ああやあ、ありがとう。私は他の提案についても読んでいますが、今のところこれで修正されています。 –

0

例えば、より多くのこのような何かを試してみてください私は一瞬のうちに下降した。私は給料に行く前に毎時または給料で読むのを忘れていました。