第四章 复合类型
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,BettySue
Grade:C
Age:22
注意,该程序应该接受的名字包含多个单词。另外,程序将向下调整成绩,即向上调一个字母。假设用户请求A、B或C,所以不必担心D和F之间的空档。
//getline 整行捕获直到遇到回车键为止
#include<iostream>
using namespace std;
int main()
{
const int size=20;
char first_name[size],last_name[size];
char grade;
int age;
cout<<"What is your first name?";
cin.getline(first_name,size);
cout<<"What is your last name?";
cin.getline(last_name,size);
cout<<"What letter grade dou you deserver?";
cin>>grade;
cout<<"What is your age?";
cin>>age;
cout<<"Name: "<<last_name<<","<<first_name<<endl;
cout<<"Grade: "<<char(grade+1)<<endl;//grade+1输出ASCII码值,char强制转换对应字母
cout<<"Age: "<<age<<endl;
return 0;
}
2、修改程序清单4.4,使用C++ string类而不是char数组。
//程序清单 4.4
#include<iostream>
int main()
{
usingnamespace std;
constint ArSize=20;
charname[ArSize];
chardessert[ArSize];
cout<<"Enteryour name:\n";
cin.getline(name,ArSize);//readthrough newline
cout<<"Enteryour favorite dessert:\n";
cin.getline(dessert,ArSize);
cout<<"Ihave some delicious"<<dessert;
cout<<"foryou, "<<name<<".\n";
return0;
}
#include<iostrea