9.2

#include <iostream>
#include <string>
#include <cctype>
using namespace std;
const int ArSize=10;
void strcount(const string &s);
void main92()
{
	string input;
	string input2;
	char next;
	cout<<"Enter a line:"<<endl;
	while(getline(cin,input))	//用getline是为了可以读取空格
	{
		if(input==" ")
			break;
		strcount(input);
		
	}

	
	cout<<"Bye"<<endl;
	system("pause");


}

void strcount(const string &str)
{
	static int total=0;
	int count=str.length();//直接调用函数
	cout<<"\n"<<str<<" contains"<<endl;
	/*int i=0;
	while(str[i]!=\'\0\')
	{	
		i++;
		count++;
	}*/

	total+=count;
	cout<<count<<"  characters\n";
	cout<<total<<" total"<<endl;
}

这道题我主要是卡在了如何读取空格的问题上,一开始我是想直接cin>>input,然后碰到空格的时候,就直接input=input+” “;

但是似乎总会有点问题,最后找到了getline输入格式,

发现对输入输出这一块还不是很多透彻,还需继续努力!

版权声明:本文为qq84435原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/qq84435/p/3664859.html