2017-11-09 1 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; 
} 

Ich bin in ein Problem, wo meine Funktion nichts tun wird. Es wird kompiliert, aber es gibt keine Ausgabe. Die Sache ist, wenn ich den Vektor sPay von allen Teilen nehme, funktioniert es vollkommen gut. Irgendwelche Vorschläge, warum der eine Teil nicht funktioniert? Von meiner begrenzten Kenntnis sollte es vollkommen gut funktionieren, aber ich kann nicht herausfinden, was das verursachen würde.Vector Function Programm Probleme

Mein Beispiel Textdatei ist

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

Ich schlage vor, Sie lesen [Warum ist Iostream :: eof innerhalb einer Schleife als falsch angesehen?] (Https://stackoverflow.com/questions/5605125/why-isiostreameof-inside-a-loop-condition-conspired) -wrong) –

+0

Ich schlage Ihnen auch vor, [nehmen Sie ein paar gute Anfängerbücher] (http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) und lernen Sie über * Strukturen * und * Klassen *, um eng verwandte Daten zu gruppieren. Und natürlich über lokale Variablen! –

+0

Fragen Sie sich das: Wenn nicht lesen 'sPay' ermöglicht das Programm zu arbeiten, muss etwas mit der Eingabe dafür falsch sein. Was lesen Sie vorher und was ist das nächste Datenelement in Ihrer Eingabedatei? – 1201ProgramAlarm

Antwort

1

Ihre Eingabedatei nicht Ihren Lesecode entspricht. Es gibt 9 Werte in der Datei, die Sie angezeigt haben, aber Ihr Code versucht nur 8 Werte zu lesen.

Wenn readIn() zu diesem Code wird:

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

Es versucht, ein double zu lesen, aber die Datei hat Hourly statt, so dass die Lese ausfällt.

Entfernen Sie also entweder die Zeile Hourly aus der Datei, oder fügen Sie einen Anruf zu iFile >> sTemp hinzu, um diese Zeile zu lesen.

Auch die Parameter string sTemp, double dTemp und int iTemp sollten als lokale Variablen anstelle von Eingabeparametern deklariert werden.

Auch readIn() macht keine Fehlerbehandlung. Ihr main() Code macht eine ungültige Annahme, dass der Vektor der Vornamen genau mit der Größe der anderen Vektoren übereinstimmt, aber readIn() garantiert das nicht.

Und zuletzt, überprüfen Sie eof() bevor Sie etwas gelesen haben, ist falsch. Das Flag eofbit des Streams wird erst aktualisiert, wenn eine Leseoperation versucht, vergangenes EOF zu lesen.

Sie sollten diesen Code erneut schreiben. Zum Beispiel versuchen, etwas mehr wie folgt aus:

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

Alternativ kann, da Ihre Datenleitung basiert, sollten Sie std::getline() verwenden, um jede Zeile zu lesen, und dann std::istringstream verwenden, um Werte zu analysieren:

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

Oh wow, danke, ich bin blind. Ich werde auch die anderen Vorschläge lesen, aber im Moment behebt das alles. –

0

Ich ging für einen Moment derp. Ich habe vergessen, die stündliche oder bezahlte vor dem Gehalt zu lesen.