我是C ++的新手,我试图创建一个程序来读取文本中输入的单词数。我使用getline来获取文本,但是它不会以回车键停止,因此我的程序无法继续执行,并且始终停留在输入单词上。我对此并不陌生,我有点迷失了,任何批评都值得赞赏。抱歉,我也是该论坛的新手。谢谢
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
int counter = 1;
string words;
vector<string> holdingstrings;
cout << "Enter no more then 100 words" << endl;
while (getline(cin, words))
holdingstrings.push_back(words);
reverse(holdingstrings.begin(), holdingstrings.end());
if (holdingstrings.size() == 0)
cout << "No words have been entered" << endl;
words = holdingstrings[0];
if (holdingstrings.size() > 100)
cout << "You have entered more then 100 words, enter less then 100 words" << endl;
for (int i = 1; i < holdingstrings.size(); i++) {
if (words[i] == ' ')
counter++;
}
cout << "There is " << words << " words in the text" << endl;
return 0;
}