2016-11-27 100 views
-3

該代碼應該從文件讀入並存儲信息。這裏的文件:使用結構和指針

5 
Franks,Tom 2 3 8 3 6 3 5 
Gates,Bill 8 8 3 0 8 2 0 
Jordan,Michael 9 10 4 7 0 0 0 
Bush,George 5 6 5 6 5 6 5 
Heinke,Lonnie 7 3 8 7 2 5 7 

現在我只專注於保存指向名稱的指針。這是我迄今爲止的代碼(忽略了我還沒有獲得的其他功能)。我必須使用員工保存姓名[row] = new Employee;和fin >>員工[row] - >名字;我只是不知道該怎麼做。

#include "stdafx.h" 
#include <iostream> 
#include <fstream> 
#include <string> 
#include <iomanip> 
#include <vector> 

using namespace std; 

struct Employee { 
string names; 
vector<int> data; 
int totalHrs; 
}; 


int fillAndTotal(vector<Employee *>&employees); 
void sort(vector<Employee *>&employees, int amount); 
void output(vector<Employee *>&employees, int amount); 



int main() 
{ 
vector<Employee *>employees; 
//vector<string>names; 
int amount = 0; 


amount = fillAndTotal(employees); 

sort(employees, amount); 

output(employees, amount); 


system("pause"); 
return 0; 

} 

int fillAndTotal(vector<Employee *>&employees) { 

int const TTL_HRS = 7; 
ifstream fin; 
fin.open("empdata.txt"); 

if (fin.fail()) { 
    cout << "ERROR"; 
} 

int sum = 0; 
int numOfNames; 
fin >> numOfNames; 
string tmpString; 
int tempInt = 0; 

vector<int>temp(8); 

for (int row = 0; row < numOfNames; row++) { 

    employees[row] = new Employee; 

    fin >> employees[row]->names; 
+2

「我不知道該怎麼做」這不是一個問題。爲了擴展這個問題,答案是:「閱讀你的C++書籍並遵循其中的例子來閱讀和處理'std :: cin',以及使用和管理向量和結構」。 –

+0

@SamVarshavchik我試過了。我已經閱讀了講座幻燈片,教科書的部分,並且我在過去的一個半小時裏一直在弄亂代碼。我不會在這裏尋找直接的答案,但有一點幫助會很好。 – Ralf

+0

當您閱讀教科書中描述如何使用'std :: vector'的部分時,您的教科書的該部分究竟如何解釋應該向向量添加新值?這不可能是你在這裏(或不這樣做)的方式。從該任務開始:向矢量正確添加新值。如果你甚至不能這樣做,忘記填充對象的實際內容。 –

回答

0

首先,您不需要指針 - 您的Employee結構是完全安全的,可以將as-in存儲在向量中。其次,在讀取這樣的面向行的數據時,很容易偏離軌道,讓讀數溢出到下一行或下溢,並且不能爲行執行足夠的讀取 - 我所做的是編寫一次讀取整行的函數,並返回包含該行的字符串流,然後我在該字符串流上執行單獨的讀取操作。

第三,最後,編寫允許您轉儲數據結構的函數總是有用的,這樣您就可以直觀地確認您已經正確地填充了結構。或者你可以使用調試器。

#include <iostream> 
#include <fstream> 
#include <sstream> 
#include <string> 
#include <vector> 

struct Employee 
{ 
    std::string names; 
    std::vector<int> data; 
    int totalHrs; 
}; 
using EmpList = std::vector<Employee>; 

int fillAndTotal(EmpList& employees); 
std::stringstream readRowData(std::istream& fin); 

#ifndef NDEBUG 
// Debug - dump to stream. 
std::ostream& operator<<(std::ostream& out, const Employee& employee); 
std::ostream& operator<<(std::ostream& out, const EmpList& employees); 
#endif 

int main(int argc, char* argv[]) 
{ 
    EmpList employees; 

    auto grandTotal = fillAndTotal(employees); 
    std::cout << employees; 
    std::cout << "\nGrand total hours: " << grandTotal << std::endl; 
} 

int fillAndTotal(EmpList& employees) 
{ 
    auto fin = std::ifstream("empdata.txt"); 

    auto rowCount = 0; 
    auto rowData = readRowData(fin); 
    rowData >> rowCount; 

    auto totalHours = 0; 
    for (auto i = 0; i < rowCount; ++i) 
    { 
     rowData = readRowData(fin); 
     if (!fin.eof()) 
     { 
      auto employee = Employee{}; 

      rowData >> employee.names; 

      int hours; 
      while (rowData >> hours) 
      { 
       if (hours != 0) 
       { 
        employee.data.push_back(hours); 
        employee.totalHrs += hours; 
        totalHours += hours; 
       } 
      } 

      employees.push_back(employee); 
     } 
    } 
    return totalHours; 
} 

std::stringstream readRowData(std::istream& fin) 
{ 
    std::stringstream rowStream; 
    std::string rowData; 
    if (getline(fin, rowData)) 
    { 
     rowStream.str(rowData); 
    } 
    return rowStream; 
} 

#ifndef NDEBUG 
std::ostream& operator<<(std::ostream& out, const Employee& employee) 
{ 
    out << "Name: " << employee.names << '\n'; 
    out << "Total hours: " << employee.totalHrs << '\n'; 
    out << "Individual hours:"; 
    for (auto const &hours : employee.data) 
    { 
     out << ' ' << hours; 
    } 
    out << std::endl; 
    return out; 
} 

std::ostream& operator<<(std::ostream& out, const EmpList& employees) 
{ 
    auto first = true; 
    for (auto const &employee : employees) 
    { 
     if (first) 
     { 
      first = false; 
     } 
     else 
     { 
      out << '\n'; 
     } 
     out << employee; 
    } 
    return out; 
} 
#endif 

現在你的輸出函數已經寫好了,你只需要編寫你的排序。