当我在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;
}
更具体地说,即使在提示输入单词时输入“退出”,我也无法退出。我该怎么解决这个问题?我知道这是noob代码:)我刚刚开始使用C ++
答案 0 :(得分: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后,打印“已完成”,然后再次“启动”。 您的代码多次调用该函数。
问题是该函数只返回一个字符串。所以这一行
string words[MAX_WORDS] = input_words(MAX_WORDS);
似乎调用函数input_words MAX_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);
答案 1 :(得分: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}$>
答案 2 :(得分:0)
问题,我认为在您的主要位置返回input_words()
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;
}
:
getline()
忘记以下内容,C ++ getline()
会自动删除'\ n'。
您是否检查过"Quit" != "Quit\n".
字是否在其末尾有换行符?
也就是说,
{{1}}