このコードは、ファイルから読み込み、情報を格納することになっています。ここにファイルがあります:構造体とポインタの使用
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
今私はちょうど名前へのポインタを保存に焦点を当てています。ここに私がこれまで持っていたコードがあります(私はまだそれらに手を加えていない他の機能を無視します)。私は従業員[行] =新しい従業員を使用して名前を保存する必要があります。およびfin >> employees [行] - >名前;私はちょうどそれをやっていく方法を知らない。
#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私はそれを試みました。私は講義のスライド、教科書のセクションを読んできました。私は過去1時間半のコードをいじっていました。私はまっすぐ上の答えを探してここに来ませんが、ちょっとした助けがよかったです。 – Ralf
'std :: vector'の使い方を説明しているテキストブックのセクションを読んだとき、教科書のそのセクションはどのようにベクトルに新しい値を追加するべきかを説明してくれましたか?ここであなたがしている(あるいはしない)方法ではありません。そのタスクから始めましょう:ベクトルに新しい値を正しく追加する。あなたもそれを行うことができない場合は、オブジェクトの実際の内容を埋めることについて忘れてください。 –