C++不需要的无限 while 循环
当我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我对这个函数进行了这样的修改:
输入Quit后,打印“finished”,然后再次“started”。
您的代码多次调用该函数。
问题是该函数仅返回一个字符串。所以该行
似乎调用了函数 input_words
MAX_WORDS
次。一个好方法是切换到
vector
:I modified the function in such a way:
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
seems to call the function input_words
MAX_WORDS
times.A good way would be to switch to
vector<string>
:我尝试了以下测试程序,它有效:
I tried the following test program, it works:
我认为问题出在你的 main 中,你返回
input_words()
的结果,它是一个string
来初始化中的 words main()
,其类型为string[]
。肯定是这个问题。重写为使用
vector
:忘记以下内容,C++
getline()
自动去除 '\n'。您是否检查过您的
getline()
单词末尾是否有换行符?那是,
The problem, I think is in your main where you return the result of
input_words()
which is astring
to initialize words inmain()
which is of typestring[]
. It's definitely this problem.Rewritten to use
vector
: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,