2017-01-25 5 views
1

の2行を読んで、私はは3ライン入力

Testing#the hash#tag 
#program!#when #beginning? a line 
or #also a #comma, 

と出力のように段落に取ることになっているプログラムを持っている

#the 
#tag 
#program 
#when 
#beginning 
#also 
#comma, 

のようなものI論理が理にかなっているように感じるが、明らかにプログラムが入力行に入らないように見えるからではない。この問題は、以下の最後のソースファイルでほぼ間違いありません。ここで

は、メインのソースプログラム

#include "HashTagger.h" 
#include <string> 
#include <iostream> 
using namespace hw02; 
using namespace std; 

int main() { 

    // Construct an object for extracting the 
    // hashtags. 

    HashTagger hashTagger; 

    // Read the standard input and extract the 
    // hashtags. 

    while (true) { 

    // Read one line from the standard input. 

    string line; 
    getline(cin, line); 
    if (!cin) { 
     break; 
    } 

    // Get all of the hashtags on the line. 

    hashTagger.getTags(line); 
    } 

    // Print the hashtags. 

    hashTagger.printTags(); 

    // Return the status. 

    return 0; 
} 

私のヘッダファイル

#ifndef HASHTAGGER_H 
#define HASHTAGGER_H 

#include <string> 

namespace hw02 { 

class HashTagger { 
public: 
    void getTags(std::string line); 

    void printTags(); 

private: 

    std::string hashtags_; 

}; 

} 

#endif 

とソースファイル内のテストプログラムは2行目のみになることを示しているようだ ソースファイルです最後の2つのハッシュタグを取得する前に停止します。

#include "HashTagger.h" 
#include <iostream> 
using namespace std; 
using namespace hw02; 

void HashTagger::getTags(string line) { 

    // Loop over all characters in a line that can begin a hashtag 
    int b = 0; 
    string hashtags_ = ""; 
    for (unsigned int j = 0; j < line.length(); ++j) { 
     char c = line.at(j); 

     // if "#" is found assign beginning of capture to b 
     if (c == '#') { 
      b = j; 

      // if the beginning is less than the end space, newline, ".", "?", or "!" found, add substring of the hashtag to hashtags_ 
     } 
     if (b < j && (c == ' ' || c == '\n' || c == '.' || c == '?' || c == '!')) { 
      hashtags_ = hashtags_ + "\n" + line.substr(b, j - b + 1); 
      b = 0; 
      //Test// cout << b << "/" << j << "/" << c << "/" << hashtags_ << "/" << endl; 
     } 
    } 
} 

void HashTagger::printTags() { 

    // print out hashtags_ to the console 
    cout << hashtags_ << endl; 
} 

答えて

0

YoあなたのgetTags機能の中でhashtags_を再宣言しています。したがって、すべての文字列の変更は、クラスメンバー変数の代わりにローカル変数で動作します。

変更ライン

string hashtags_ = "";

hashtags_ = "";

再宣言を回避し、後に出力するために使用されるクラスのメンバ変数上で動作するためです。また


、早すぎるメインループの外にINGのbreakを避けるため、またはgetTagsコールの後、あなたの小切手とbreak文を移動するために、あなたの入力は2つの改行文字(\n\n)で終了していることを確認してください:

while (true) { 

    // Read one line from the standard input. 
    string line; 
    getline(cin, line); 

    // Get all of the hashtags on the line. 
    hashTagger.getTags(line); 

    if (!cin) { 
     break; 
    } 
    } 
+0

良いキャッチで簡単な質問です。 hashtags_に格納された文字列はprintTags()に呼び出されますか?メインプログラムでは出力が空白で、取得できる唯一の出力はgetTagsメソッドからのものだからです。 – Vyff

+0

再宣言を削除すると、それが実行されます。次に、オブジェクトのメンバ変数を操作し、同じオブジェクトに対して 'getTags'と' printTags'を呼び出すので、文字列は「持ち越し」ます。 – DevCybran

+0

@Vyff 3行目が決して処理されないという考えられる原因を追加しました。問題のために – DevCybran

関連する問題