2016-04-25 49 views
0

私は大学入学システムをエミュレートするC++プログラムを作成しようとしています。学生はIDを入力し、テキストファイルで情報を検索し、テキストファイル。私は、登録されたコースを構造体の配列に取得するのに問題が発生している点に気づきました。 getline関数を使用して、 '、'をデリミアとして使用すると、次のカンマまで次の行を引き継ぎます。これに対して正しいアルゴリズムは何でしょうか?コンマで区切られたテキストファイルを配列に読み込む

これは学生情報が含まれているファイルのセットアップです:

  • 918273645、スティーブ、オルブライト、ITCS2530、MATH210、ENG140
  • 123456789、キム、マーフィー、ITCS2530、MATH101
  • 213456789 、ディーン、バウアーズ、ITCS2530、ENG140
  • 219834765、ジェリー、クラーク、MGMT201、MATH210

(レイアウト用の箇条書きが追加されました。

たとえば、ユーザーは自分のIDに「123456789」と入力し、Kim Murphyの情報が読み取られます。 getlineの最初の反復で、 "ITCS2530"が読み込まれ、変数に入れられ、次に構造体にロードされます。問題はない。しかし、リストの最後のコースでは、次のコンマの前に改行文字があるので、次の繰り返しは "MATH101/nl213456789"と読み込み、文字列全体を変数に入れて構造体にロードしようとします。

最初の列はIDで、次に姓、姓、その後に現在登録されている列です。登録されたコースの数はさまざまです。任意の助けを事前に

student login() 
{ 
    string ID; 
    student newStudent; 
    string enrolled; 
    int i = 0; 
    while (true) 
    { 
     cout << "Enter Student ID: "; 
     cin >> newStudent.ID; 
     cout << endl; 
     if (newStudent.ID.length() == 9) 
      break; 
     else 
      cout << "That ID is invalid - IDs are 9 digits" << endl; 
    } 
    ifstream inFile; 
    ofstream outFile; 
    inFile.open("registration.txt"); 
    if (inFile.is_open())                
    //Check if file is open 
    { 
     while (!inFile.eof())               
     //While not at end of file 
     { 
      getline(inFile, ID, ',');             
      //Search for ID 
      if (ID == newStudent.ID) 
      { 
       getline(inFile, newStudent.fName, ',');           
       //Assign fName and lName 
       getline(inFile, newStudent.lName, ','); 

       while (enrolled != "\n") 
       { 
        getline(inFile, enrolled, ','); 
        if (enrolled == "\n") 
        { 
         cout << "Not currently enrolled in a class." << endl; 
        } 
        else 
        { 
         newStudent.courses[i] = enrolled; 
         i++; 
        } 
       } 
        cout << newStudent.lName << ", Welcome to the MCC Enrollment System!" << endl; 
       for (i = 0; i <= NUM_OF_COURSES; i++) 
       { 
        cout << "Enrolled courses: " << newStudent.courses[i] << endl; 
       } 

        cout << endl; 
        break; 
        //Stops searching 
      } 
      else                   
       //Ignores rest of line - used to skip to the next line 
      { 
       getline(inFile, ID, '\n'); 
      } 
      if (inFile.eof())               
       //If ID was not found 
      { 
       inFile.close(); 
       cout << "Enter First Name: ";           
       //Begin entry of new student 
       cin >> newStudent.fName; 
       cout << endl; 
       cout << "Enter Last Name: "; 
       cin >> newStudent.lName; 

       cout << endl; 
       outFile.open("registration.txt", ios::app); 
       if (outFile.is_open()) 
       { 
        outFile << newStudent.ID << "," << newStudent.fName << "," << newStudent.lName << "\n"; 
       } 
      } 
     } 
    } 
    return newStudent; 
} 

ありがとう:ここ

は、私が現在働いているコードです。

+0

あなたが持っているコードを表示できますか? – Default

+0

ファイルからどのようにデータを取得するかを確認する必要があります。 fgets、fscanf、std :: cin、std :: getlineのすべてが必要なのは、改行または空白のいずれかでデータを取得するためです。 –

+0

@JerryJeremiah私が取り組んでいるコードのスニペットで質問を更新しました。 –

答えて

2

問題は、std :: getlineは区切り文字として1文字だけを使用することです。デフォルトは改行ですが、別の文字を使用すると、改行文字は区切り文字ではなくなり、テキストに改行が入ります。

答えは、std :: getlineをデフォルト(改行)デリミタで文字列に読み込み、文字列ストリームを使用してそのテキスト行を保持し、std :: getlineをカンマで呼び出すことができるようにすることですデリミタとして。このような

何か:

#include <fstream> 
#include <sstream> 
#include <iostream> 
#include <vector> 

int main() 
{ 
    std::ifstream inFile("registration.txt"); 
    if (inFile.is_open()) 
    { 
     std::string line; 
     while(std::getline(inFile,line)) 
     { 
      std::stringstream ss(line); 

      std::string ID, fname, lname; 
      std::getline(ss,ID,','); std::cout<<"\""<<ID<<"\""; 
      std::getline(ss,fname,','); std::cout<<", \""<<fname<<"\""; 
      std::getline(ss,lname,','); std::cout<<", \""<<lname<<"\""; 

      std::vector<std::string> enrolled; 
      std::string course; 
      while(std::getline(ss,course,',')) 
      { 
       enrolled.push_back(course); std::cout<<", \""<<course<<"\""; 
      } 
      std::cout<<"\n"; 
     } 
    } 
    return 0; 
} 

この例では、私はあなたが読まれるかを見ることができるように引用符で囲まれ、画面にテキストを書いています。

+0

!どうもありがとうございます! –

0
split(string, seperator) 

split("918273645,Steve,Albright,ITCS2530,MATH210,ENG140", ",") 
split("123456789,Kim,Murphy,ITCS2530,MATH101", ",") 
split("213456789,Dean,Bowers,ITCS2530,ENG140", ",") 
split("219834765,Jerry,Clark,MGMT201,MATH210", ",") 

私はC++をほとんど知りませんが、このコマンドを覚えています。

+0

'split'の実装を覚えていますか? – Default

+0

http://stackoverflow.com/questions/236129/split-a-string-in-c – Anuga

+0

これはファイルからの改行を処理しますか? – Default

関連する問題