2012-08-01 77 views
1

我正試圖編寫一個程序,不斷從用戶處獲取輸入信息,直到用戶輸入「quit」。每次用戶輸入時,我都希望程序輸出用戶輸入的單詞數量。因此,用戶的部分下面輸入:如何讓用戶輸入一個C++程序來計算單詞的數量?

hello how are you 

會產生以下的輸出:

You entered 4 words. 

但是,我無法寫程序,以便它依靠的只有一行字的數量;它在進入下一行之前不清除數字。因此,如果三次從用戶那裏獲得輸入,則會在這三行上加上單詞的總數。例如,下面的輸入:

how are you 
i am good thank you 
quit 

將產生以下輸出:

You entered 9 words. 

當我希望它輸出字的以下各行的用戶輸入(除了退出)的數量,即

>>how are you 
<<You entered 3 words. 
>>i am good thank you 
<<You entered 5 words. 
>>quit 

這裏是我的代碼的相關位:

char *input; 
int inum; 

int inputLoop() 
{ 
    char quit[] = "quit"; 
    inum = 0; //counts number of words 

    while (strcmp(input, quit) != 0) 
    { 
     cin >> input; 
     inum++; 
    } 
    cout <<"You entered " <<inum <<" words." <<endl; 

我寧可不使用類似矢量的東西;無論我使用什麼都需要最終轉換爲* char,因爲我的全局變量是* char。 (而我的全局變量是* char,因爲根據特定的條件,輸入可能會被設置爲* argv [] from main。)

我已經嘗試了各種各樣的東西,但我無法通過strcmp(input,quit)一次比較輸入的一個字而不是比較整個輸入行以退出的事實。幫幫我。

回答

1

您應該使用getline()將一整行輸入到某個緩衝區中。然後,處理該輸入的緩衝區以計算其中的字數。假設您將每個單詞定義爲由空格分隔的字符塊。我自己,我是strtok()分手緩衝區的粉絲。

+1

@DeadMG:他提議'getline'。只要你知道它不是線程安全的,'strtok'在C中是完全正確的。 – netcoder 2012-08-01 17:40:16

+1

@netcoder:除了'strtok'需要一個可變的C風格字符串,並沒有從C++'std :: string'得到一個明智的方法。它也不是線程安全的 - 對於OP的特殊情況來說,這可能是也可能不是一個問題,但會使其無法使用。 – 2012-08-01 17:43:18

+0

我低估了一個完美的理由,並明確說明了原因。這是完全有建設性的。 – Puppy 2012-08-01 17:46:07

5

您的任何要求都不能阻止使用std::stringstd::vector。我建議你使用它們。

#include <string> 
#include <sstream> 
#include <iostream> 
#include <vector> 

std::vector<std::string> words; 

int inputLoop() 
{ 
    char quit[] = "quit"; 
    total_words = 0; 

    std::string line; 
    // grab a line at a time 
    while(std::getline(std::cin, line) && line != quit) { 
     // clear the vector of words 
     words.clear(); 
     // make a string stream to read words from that line 
     std::stringstream ss(line); 
     // grab all the words into a vector 
     std::string word; 
     while(ss >> word) { 
      words.push_back(word); 
     } 
     std::cout <<"You entered " <<words.size() <<" words." <<endl; 
    } 
} 

int main(int argc, char** argv) { 
    // get the data from argv 
    words = std::vector<std::string>(argv, argv + argc); 
} 
+0

你也不清楚'單詞'矢量。 :P – Puppy 2012-08-01 17:35:15

+0

哦,很好。謝謝。 – 2012-08-01 17:36:31

+0

@DeadMG,然後你的觀點在哪裏呢? – 2012-08-01 17:37:22

0

另一種做法,只是爲了好玩:

#include <iostream> 
#include <algorithm> 
#include <iterator> 

int main() 
{ 
    unsigned num = 0; 
    std::for_each(
     std::istream_iterator<std::string>(std::cin), 
     std::istream_iterator<std::string>(), 

     [&num](const std::string& s) 
     { 
      if (s == "quit") 
       std::cin.setstate(std::ios::eofbit); 
      ++num; 
      if (std::cin.peek() == '\n') { 
       std::cout << "You entered " 
          << num 
          << " word" 
          << ((num == 1) ? "." : "s.") 
          << '\n'; 
       num = 0; 
      } 
     }); 
} 

不被標記化而行成矢量:)

0

我會打電話的距離浪費資源

#include <string> 
#include <algorithm> 
#include <iterator> 
#include <iostream> 
#include <sstream> 

int main() 
{ 
    std::string line; 
    while(std::getline(std::cin, line) && line != "quit") 
    { 
     std::stringstream linestream(line); 
     std::cout << "You entered " 
        << std::distance(std::istream_iterator<std::string>(linestream), 
            std::istream_iterator<std::string>()) 
        << " words\n"; 
    } 
} 
+0

你是不是指'距離'? – jrok 2012-08-01 18:45:57

+0

是的。這就是我的意思。 – 2012-08-01 18:50:49

相關問題