第九章2-3题

来源:互联网 发布:smtp 端口 编辑:程序博客网 时间:2024/05/18 20:11
第2题

#include<string>
#include<iostream>
using namespace std;
void strCount(const string& str);

int main()
{
int i = 1;
string nx;
while(1)
{
cout<<"请输入第"<<i<<"个句子.(q To exit)";
getline(cin,nx);
if(nx == "q")
break;
else strCount(nx);
++i;
}
cout<<"Bye";
return 0;
}
void strCount(const string& str)
{
static int total = 0;
total += str.size();
cout<<"总共输入了"<<total<<"个字母."<<endl;
}


第3题

#include<iostream>
#include<cstring>
struct chaff
{
char dross[20];
int slag;
};
int main()
{
using namespace std;
chaff* rb = new chaff[10];
for(int i = 0 ; i < 10 ; ++i)
{
cout<<"请输入第"<<i+1<<"个渣渣的名称:";
cin.getline((rb+i)->dross,20);
cout<<"请输入这个渣渣的战斗力:";
cin>>(rb+i)->slag; //均没有检查输入
cin.get();
}
for(int i = 0 ; i < 10 ; ++i)
{
cout<<"第"<<i+1<<"个渣渣名叫"<<(rb+i)->dross<<endl;
cout<<"该渣渣战斗力为:"<<(rb+i)->slag<<endl;
}
delete [] rb;
return 0;
}

0 0
原创粉丝点击