-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;
“我不知道该怎么做”这不是一个问题。为了扩展这个问题,答案是:“阅读你的C++书籍并遵循其中的例子来阅读和处理'std :: cin',以及使用和管理向量和结构”。 –
@SamVarshavchik我试过了。我已经阅读了讲座幻灯片,教科书的部分,并且我在过去的一个半小时里一直在弄乱代码。我不会在这里寻找直接的答案,但有一点帮助会很好。 – Ralf
当您阅读教科书中描述如何使用'std :: vector'的部分时,您的教科书的该部分究竟如何解释应该向向量添加新值?这不可能是你在这里(或不这样做)的方式。从该任务开始:向矢量正确添加新值。如果你甚至不能这样做,忘记填充对象的实际内容。 –