9.2
#include
#include
#include
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输入格式, 发现对输入输出这一块还不是很多透彻,还需继续努力! ———————————————————————————————————————————————————————————————————————————— next为多余变量,已改正,感谢 wuyalu123 提醒。 ————-2014.9.4 ————————————————————————————————————————————————————————————————————————————
—————————————————————————————————————————————————— //写的错误或者不好的地方请多多指导,可以在下面留言或者给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。 转载请注明出处:https://www.royalchen.com/ author:royalchen Email:royalchen@royalchen.com ———————————————————————————————————————————————————
- 本文作者: royalchen
- 本文链接: http://www.royalchen.com/2016/02/24/cprimerplus第六版课后编程题答案92/
- 版权声明: 本博客所有文章除特别声明外,均采用 MIT 许可协议。转载请注明出处!