0

C++ で次のコードを使用すると無限ループが発生しますが、その理由がわかりません。問題はinput_words()関数内にあると思われます。コードは次のとおりです。

#include<iostream>
using namespace std;

string input_words(int maxWords) {
    int nWord = 0;
    string words[maxWords];
    string aWord = "";
    while (aWord != "Quit" && nWord < maxWords) {
        cout << "Enter a number ('Quit' to stop): ";
        getline (cin, aWord);
        words[nWord] = aWord;
        nWord++;
    }
    return *words;
}

int num_words (string words[], int maxWords) {
    int numWords = 0;
    for (int i=0; i<maxWords; i++) {
        if (words[i] == "Quit") {
            break;
        }
        numWords++;
    }
    return numWords;
}

int main() {

    const int MAX_WORDS = 100;
    string words[MAX_WORDS] = input_words(MAX_WORDS);

    int lenWords = num_words(words, MAX_WORDS);
    cout << "\nThere are " << lenWords << " words:\n";

    for (int i=0; i<MAX_WORDS; i++) {
        if (words[i] == "Quit") {
            break;
        }
        cout << words[i] << "\n";
    }
    return 0;
}

具体的には、単語の入力を求められたときに「Quit」と入力しても終了できません。どうすればこれを解決できますか?私はこれがnoobコードであることを知っています:)私はC ++を始めたばかりです

4

3 に答える 3

2

私はそのような方法で関数を修正しました:

string input_words(int maxWords) {
    cout << "started" << endl;
    int nWord = 0;
    string words[maxWords];
    string aWord = "";
    while (aWord != "Quit" && nWord < maxWords) {
        cout << "Enter a number ('Quit' to stop): ";
        getline (cin, aWord);
        words[nWord] = aWord;
        nWord++;
    }
    cout << "finished" << endl;
    return *words;
}

Quit を入力すると、"finished" と出力されてから、再び "started" と出力されます。あなたのコードは関数を数回呼び出しています。

問題は、関数が 1 つの文字列しか返さないことです。だからライン

string words[MAX_WORDS] = input_words(MAX_WORDS);

関数input_wordsMAX_WORDS回を呼び出すようです。

良い方法は、に切り替えることvector<string>です:

vector<string> input_words(int maxWords) {
    vector<string> words;
    string aWord;
    while (aWord != "Quit" && nWord < maxWords) {
        cout << "Enter a number ('Quit' to stop): ";
        getline (cin, aWord);
        words.push_back(aWord);
    }
    return words;
}

...
vector<string> words = input_words(MAX_WORDS);
于 2010-02-20T20:18:25.447 に答える
0

次のテストプログラムを試しましたが、動作します:

{0,506}$> cat testcin.cpp && make testcin && ./testcin.exe
#include <iostream>
using namespace std;

int main()
{
    const int maxWords = 5;
    int nWord = 0;
    string words[maxWords];
    string aWord = "";
    while (aWord != "Quit" && nWord < maxWords) {
        cout << "Enter a number ('Quit' to stop): ";
        getline (cin, aWord);
        words[nWord] = aWord;
        nWord++;
    }    
}

make: `testcin' is up to date.
Enter a number ('Quit' to stop): test
Enter a number ('Quit' to stop): Quit

[Vlad@rabbit] [20:53:53] [~/c++]
{0,507}$> 
于 2010-02-20T19:55:00.313 に答える
0

問題は、あなたのメインにあると思いますが、その結果input_words()は aであり、その中の単語string初期化するタイプです。間違いなくこの問題です。main()string[]

を使用するように書き直しましたvector:

#include<iostream>
#include<vector>
#include<string>
using namespace std;

vector<string> input_words(int maxWords) {
    int nWord = 0;
    vector<string> words;
    string aWord = "";
    while (aWord != "Quit" && nWord < maxWords) {
        cout << "Enter a number ('Quit' to stop): ";
        getline (cin, aWord);
        words.push_back(aWord);
        nWord++;
    }
    return words;
}

int num_words (vector<string> words) {
    // return words.size();

    int numWords = 0;
    vector<string>::iterator it = words.begin();
    for (; it != words.end(); it++) {
        if (*it == "Quit") {
            break;
        }
        numWords++;
    }
    return numWords;
}

int main() {

    const int MAX_WORDS = 100;
    vector<string> words = input_words(MAX_WORDS);

    int lenWords = num_words(words);
    cout << "\nThere are " << lenWords << " words:\n";

    vector<string>::iterator it = words.begin();
    for (; it != words.end(); it++) {
        if (*it == "Quit") {
            break;
        }
        cout << *it << endl;
    }
    return 0;
}

次のことは忘れてください。C++getline()は '\n' を自動的に取り除きます。

getline()単語の末尾に改行があるかどうかを確認しましたか? あれは、

"Quit" != "Quit\n".
于 2010-02-20T19:55:03.013 に答える