第四章 复合类型
1,编写一个C++程序,输出如下:
What is your first name? Betty Sue
What is your last name? Yewe
What letter grade do you deserve? B
What is your age? 22
Name: Yewe, Betty Sue
Grade: C
Age: 22
程序将向下调整程序
#include<iostream>
uisng namespace std;
int main()
{
char first_name[20],last_name[20];
char grade;
int age;
cout<<"What is your first name?";
cin>>getline(first_name,20);
cout<<"What is your last name?";
cin>>getline(last_name,20);
cout<<"What letter grade do you deserve?";
cin>>grade;
cout<<"What is your age?";
cin>>age;
cout<<"Name: "<<first_name<<" , "<<last_name<<endl;
cout<<"Grade: "<<char(grade+1)<<endl;
cout<<"Age: "<<age<<endl;
return 0;
}
2,修改程序清单4.4,使用C++string类而不是char数组。
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
int main()
{
string name;
string dessert;
cout<<"Enter your name:\n";
getline(cin,name);
cout<<"Enter your favorite dessert:\n";
getline(cin,dessert);
cout<<" I have some delicious "<<dessert;
cout<<" for you, "<<name<<".\n";
return 0;
}