2009-05-30 15 views
1
#include <iostream> 
#include <string> 
#include <fstream> 

using namespace std ; 


string strWord(int index , string line) 
{ 
     int count = 0; 
     string word; 
     for (int i = 0 ; i < line.length(); i++) 
     { 
      if (line[i] == ' ') 
      { 
       if (line [i+1] != ' ') 
       { 

        count ++; 
        if ( count == index) 
        { 
         return word; 
        } 
        word =""; 
       } 
      } 
      else 
      { 
       word += line[i]; 
      }  
     } 
} 







int main () 
{ 
    ifstream inFile ; 
    inFile.open("new.txt"); 
    string line , id; 

    cout <<"Enter id : "; 
    cin >>id; 
    while(!inFile.eof()) 
    { 
         getline (inFile , line); 
         if (strWord (1, line) == id) 
         { 
          cout <<strWord (2 , line) <<endl; 
          break; 
         } 
    } 
    system("pause"); 
} 

質問です:誰かが私はそれが何をしているかを取得しない私には、これを説明することができ、私はコンセプトを取得しますが、それぞれの行が何をやっている意味ですか?ファイルの行を取得し、行の2番目の単語を読み取るC++コード?

+1

のコメントを望んでいました。この宿題ですか? – SingleNegationElimination

+0

フォーマットを修正しました。 @ H4cKL0rD、それを4つのスペースをインデントすることでスタックオーバーフローに貼り付けるときにコードをフォーマットすることを忘れないでください。 –

答えて

3

あなたは、各行の前に4つのスペースを使用するようにコードを修正してください各行

// function that returns a word from 'line' with position 'index' 
// note that this is not a zero based index, first word is 1, 
// second is 2 etc .. 
string strWord(int index, string line) 
{ 
    int count = 0; // number of read words 
    string word; // the resulting word 
    for (int i = 0 ; i < line.length(); i++) { // iterate over all characters in 'line' 
     if (line[i] == ' ') { // if this character is a space we might be done reading a word from 'line' 
      if (line[i+1] != ' ') { // next character is not a space, so we are done reading a word 
       count++; // increase number of read words 
       if (count == index) { // was this the word we were looking for? 
        return word; // yes it was, so return it 
       } 
       word =""; // nope it wasn't .. so reset word and start over with the next one in 'line' 
      } 
     } 
     else { // not a space .. so append the character to 'word' 
      word += line[i]; 
     }  
    } 
} 


int main() // main function of the program, execution starts here 
{ 
    ifstream inFile; // construct input file stream object 
    inFile.open("new.txt"); // associate the stream with file named "new.txt" 
    string line, id; // 

    cout << "Enter id : "; // write "Enter id :" to console 
    cin >> id; // read input from console and put the result in 'id' 

    while (!inFile.eof()) { // do the following as long as there is something to read from the file 
     getline(inFile, line); // read a line from the file and put the value into 'line' 
    if (strWord(1, line) == id) { // if the first word in 'line' equals 'id' .. 
     cout << strWord(2, line) << endl; // prints the second word in 'line' 
     break; // exits the while loop 
    } 
    } 

    system("pause"); // pause the program (should be avoided) 
} 
+0

文字列の最後の単語を検索しようとしている場合を除き、これは機能します。実行時に 'line [i + 1]'にアクセスするとクラッシュします。 – kleinfreund

2

私は非常に慎重に読んでいないが、それは最初の単語は、ユーザーが入力した一つであるためにラインの2番目の単語を出力ように見えます。

1

は、あなたがC++のための対話型デバッガを持っていますか?たとえば、このコードをMicrosoft Visual C++にロードすると、一度に1つのステートメントを実行し、各変数の値を調べることができます。これは、新しいコードが詳細レベルで何をするのかを知るうまい方法です。

0

があり、文からすべてのインデックスを持つ単語を取るためにstrWordと呼ばれる機能であり、それを返します。

メインプログラムは、各行にidと別の単語を含むファイルを開き、読み込みの準備を整えます。 idは、ファイル内の行のいずれかの最初の単語である場合

そしてそれは、ユーザからIDのチェックを依頼します。そうであれば、その行から2番目の単語を取り出して印刷します。

それだけです。特定の行または一連の行に問題があるかどうかを確認します。

それともグレッグが示唆するように、デバッガのために行きます。これは、どの論理をも理解する最も簡単な方法です。

2

は多分これは役立ちます:

// return word[index] from line 
string strWord(int index , string line) // word index and actual line 
{ 
    int count = 0; // current word index 
    string word; // word buffer (return value) 

    for (int i = 0 ; i < line.length(); i++) // loop through each character in line 
    { 
    if (line[i] == ' ') // if current character is a space 
    { 
     if (line [i+1] != ' ') // but next char is not space, we got a word 
     { 
     count ++; // incr current word index counter in line 
     if ( count == index) // if count == looked for index return word 
     { 
      return word; 
     } 
     word =""; // otherwise reset word buffer 
     } 
    } 
    else // character is not space 
    { 
     word += line[i]; // add letter/digit to word buffer 
    }  
    } 
} 

仮定の数は約ここにあるように、例えば行が空白で始まる場合、アルゴリズムは、上記の失敗で、データがない場合の取り扱いのように対処する必要があります索引が無効であるか、またはインラインが空の場合。関数が失敗した場合は、最後の戻り値もありません。

関連する問題