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;
}

更具体地说,即使在提示输入单词时键入“退出”,我也无法退出。我怎么能解决这个问题?我知道这是菜鸟代码 :) 我刚开始使用 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;
}

输入退出后,打印“完成”,然后再次“开始”。您的代码多次调用该函数。

问题是该函数只返回一个字符串。所以这条线

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

我认为问题出在你的 main 中,你返回的结果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 回答