2017-02-17 4 views
-1

まず、コードが不適切か間違っている場合は、ごめんなさい。私はC++で苦労しています。オブジェクトのインスタンス化とベクトルからのアイテムの保存

ファイルから顧客を作成し、各顧客をオブジェクト顧客として保存し、それぞれの変数を保存しようとしています。

ファイルを開いて読み込み、単語をベクターに保存できました。私は今、顧客を作り、情報を保存する必要があります。

私の質問は - 新しい顧客を作成し、最初の項目をfName、2番目をlName、3番目をacctNumberとして、どのようにしてすべての情報を持つベクトルを得ることができましたか。ベクトルの4番目の項目は、新しい顧客をfNameなどで保存することです。

私が使用しているテキストファイルの例は以下のとおりです。
マイケル・ジャクソン1
ジョージ・ジョーンズ2
ブルターニュスピアーズ3

目標:3人の顧客をインスタンス化し、後で使用するために彼らのfNameを、LNAMEとacctNumberのそれぞれを設定するファイルの上。

class CustomerType { 
public: 
    CustomerType(string f, string l, int a); 
    string fName; 
    string lName; 
    int acctNumber; 
    videoType chkdOut; 
private: 
}; 

CustomerType::CustomerType(string f, string l, int a) { 
    fName = f; 
    lName = l; 
    acctNumber = a; 
} 

void createCustomer() { 
    vector<string> myVector; 
    ifstream myfile; 
    myfile.open("custDat.txt"); 
    if (myfile.is_open()) 
    { 
     while (!myfile.eof()) 
     { 
      string tempString; 
      getline(myfile, tempString); 
      istringstream iss(tempString); 
      string word; 

      while (iss >> word) { 
       myVector.push_back(word); 
      } 
     } 
     myfile.close(); 
    } 
    else 
     cout << "Can't open file" << endl; 
} 
+1

私が自分のプロジェクトだったなら、構造体を読み書きするために 'operator >>'と '<<'を書くでしょう。 – NathanOliver

+0

私はcustomerTypeの新しいオブジェクトをインスタンス化する前にコンストラクタが必要だと思って、gettersとsettersを使って変数を保存します。もし誰かが私を始めさせる手助けをしたら、私は進めることができると思う。 – Asuu

+0

_ @ Asuu_ @ Nathanの助言を求めてください。コンストラクタを定義することも良い考えです。すべてのメンバー変数を初期化するためのパラメータを提供します。 –

答えて

0

まず、必要な情報を取るであろう、あなたの顧客クラスにコンストラクタを追加します。

class customerType { 
public: 
    customerType(string firstName, string lastName, int accountNumber); 
    string fName; 
    string lName; 
    int acctNumber; 
    videoType chkdOut; 
private: 
}; 

コンストラクタは、このように定義されます。

customerType(string firstName, string lastName, int accountNumber) 
{ 
    fName = firstName; 
    lName = lastName; 
    acctNumber = accountNumber; 
} 

あなたがする必要があります各行から異なる情報を取得するために、文字列と文字を分割するメソッドを作成します。

vector<string> split(string line, char c) 
{ 
    const char *str = line.c_str(); 

    vector<string> result; 

    do 
    { 
     const char *begin = str; 

     while (*str != c && *str) 
      str++; 

     result.push_back(string(begin, str)); 
    } 
    while (0 != *str++); 

    return result; 
} 

次に、顧客を作成するための方法では、あなたがそのコンストラクタを使用して新しいオブジェクトを作成することができ、その後、顧客とのベクトルを返す:

vector<customerType> createCustomer() { 

    // create a vector of customers: 
    vector<customerType> customers; 

    vector<string> myVector; 
    ifstream myfile; 
    myfile.open("custDat.txt"); 
    if (myfile.is_open()) 
    { 
     while (!myfile.eof()) 
     { 
      string tempString; 
      getline(myfile, tempString); 

      // Here you get a vector with each work in the line 
      vector<string> splittedString = split(tempString, ' '); 

      //Finally here you create a new customer 
      customers.push_back(customerType(splittedString[0], splittedString[1], stoi(splittedString[2]))); 
     } 
     myfile.close(); 
    } 
    else 
     cout << "Can't open file" << endl; 

    return customers; 
} 

申し訳ありませんが、私はあなたが保存する方法を変更言葉。

stoi関数は、文字列を整数に変換します。

+0

これはまさに私が探しているものです! @Simon Barbieri – Asuu

+0

@Simoneここから始めてください:['while(!myfile.eof())'](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition -considered-wrong) –

+1

@πάνταῥεῖはい、ありますが、私は彼の質問に答えるために、投稿された機能を編集しました! –

関連する問題