C++primer plus第六版课后编程题答案9.2
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输入格式,
发现对输入输出这一块还不是很多透彻,还需继续努力!

浙公网安备 33010602011771号