2016-11-27 98 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 

现在你的输出函数已经写好了,你只需要编写你的排序。