C++不需要的无限 while 循环

发布于 2024-08-21 22:38:25 字数 1174 浏览 10 评论 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++

I get an infinite loop when I use the following code in C++ and I don't understand why. I suspect the problem is within the input_words() function. Here is the code:

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

More specifically, I can't exit even when I type 'Quit' when prompted for a word. How could I solve this? I know this is noob code :) I'm just starting on C++

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你的他你的她 2024-08-28 22:38:25

我对这个函数进行了这样的修改:

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”。
您的代码多次调用该函数。

问题是该函数仅返回一个字符串。所以该行

string words[MAX_WORDS] = input_words(MAX_WORDS);

似乎调用了函数 input_words MAX_WORDS 次。

一个好方法是切换到 vector

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

I modified the function in such a way:

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

After inputting Quit, it prints "finished", and then "started" again.
Your code is calling the function several times.

The problem is that the function returns only one string. so the line

string words[MAX_WORDS] = input_words(MAX_WORDS);

seems to call the function input_words MAX_WORDS times.

A good way would be to switch to 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);
你的往事 2024-08-28 22:38:25

我尝试了以下测试程序,它有效:

{0,506}
gt; 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}
gt; 

I tried the following test program, it works:

{0,506}
gt; 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}
gt; 
沉默的熊 2024-08-28 22:38:25

我认为问题出在你的 main 中,你返回 input_words() 的结果,它是一个 string 来初始化 中的 words 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".

The problem, I think is in your main where you return the result of input_words() which is a string to initialize words in main() which is of type string[]. It's definitely this problem.

Rewritten to use 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;
}

Forget the following, the C++ getline() strips the '\n' automatically.

Have you checked to see if your getline() words have newlines at the end of them?
That is,

"Quit" != "Quit\n".
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文