连续输入多个单词,然后统计这些单词的长度,以及单词个数
#include<iostream>
#include<windows.h>
#include<string>
using namespace std;
int main()
{
string word;
int count = 0;
int length = 0;
cout << "请输入任意多个单词:" << endl;
while (1)
{
if ((bool)(cin >> word) == 0) //用if((cin>>word)==0)此编译器(vs2013)报错
{
break;
}
count++;
length += word.length();
}
cout << "一共有" << count << "个单词" << endl;
cout << "总长度为:" << length << endl;
system("pause");
return 0;
}
我们可以输入自己想要输入的单词,中间可以有空格隔开,当输入结束时按回车,在按下Ctrl+z后再回车,就跳出while(1)循环执行后面的语句