zoukankan      html  css  js  c++  java
  • 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<<"
    "<<str<<" contains"<<endl;
    	/*int i=0;
    	while(str[i]!='')
    	{	
    		i++;
    		count++;
    	}*/
    
    	total+=count;
    	cout<<count<<"  characters
    ";
    	cout<<total<<" total"<<endl;
    }

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

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

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

  • 相关阅读:
    acme.sh 申请let's encrypt证书
    Excel 函数
    mysql索引失效的情况
    mysql之EXPLAIN优化分析
    mysql索引
    mysql视图
    mysql数据类型
    mysql约束
    mysql库和表的管理
    mysql的DML语言(增删改)
  • 原文地址:https://www.cnblogs.com/qq84435/p/3664859.html
Copyright © 2011-2022 走看看