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

今私はちょうど名前へのポインタを保存に焦点を当てています。ここに私がこれまで持っていたコードがあります(私はまだそれらに手を加えていない他の機能を無視します)。私は従業員[行] =新しい従業員を使用して名前を保存する必要があります。および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; 
+2

「私はそれをやる方法がわかりません」という質問はありません。それを拡張するには答えがあります: "あなたのC++の本を読んで、その中の例を読んで' std :: cin'から読んだり処理したり、ベクトルや構造を使い、管理したりしてください。 –

+0

@SamVarshavchik私はそれを試みました。私は講義のスライド、教科書のセクションを読んできました。私は過去1時間半のコードをいじっていました。私はまっすぐ上の答えを探してここに来ませんが、ちょっとした助けがよかったです。 – Ralf

+0

'std :: vector'の使い方を説明しているテキストブックのセクションを読んだとき、教科書のそのセクションはどのようにベクトルに新しい値を追加するべきかを説明してくれましたか?ここであなたがしている(あるいはしない)方法ではありません。そのタスクから始めましょう:ベクトルに新しい値を正しく追加する。あなたもそれを行うことができない場合は、オブジェクトの実際の内容を埋めることについて忘れてください。 –

答えて

0

まず、ポインタを必要としません。従業員構造は、as-inをベクターに格納するのは完全に安全です。

第2に、このようなライン指向のデータを読むときは、オフトラックして次の行にオーバーフローさせたり、アンダーフローさせたり、行の読み込みを十分に行わないことは非常に簡単です。一度に1行全体を読み込んでその行だけを含む文字列ストリームを返す関数を書くと、私はその文字列ストリームで個人的な読み込みを行います。

最後に、構造を正しく配置したことを視覚的に確認できるように、データ構造をダンプできます。または、デバッガを使用することもできます。

#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 

あなたの出力関数は既にあなたのために書かれており、あなたの並べ替えを書く必要があります。