【发布时间】:2014-04-19 07:19:39
【问题描述】:
q.编写一个程序,该程序使用一个 char 数组和一个循环来一次读取一个单词 直到输入完成这个词。然后程序应该报告 输入的字数(不计算完成)。
我希望计数递增,直到在字符串中找到 done。我在网上找到了这个问题的解决方案
#include <iostream>
#include <string>
int main()
{
using namespace std;
string word;
string matchword = "done";
int numwords=0;
cout << "Enter words (to stop, type the word done):\n";
cin >> word;
while(word != matchword)
{
cin >> word;//how does it read the next word ????
numwords++;
};
cout << "\nYou entered a total of " << numwords << " words.";
cin.get();
cin.get();
return 0;
}
cin 如何读取字符串的下一个单词。我的问题基本上是有人向我解释 cin 和字符串的工作原理。
【问题讨论】: