【C++ Primer Plus学习记录】第4章编程练习

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

注意,该程序应该接受的名字包含多个单词。另外,程序将向下调整成绩,即向上调一个字母。假设用户请求A,B或C,所以不必担心D和F之间的空挡。

#if 1
#include
using namespace std;

int main()
{
	const int size = 20;
	char first_name[size];
	cout << "What is your first name? ";
	//cin >> first_name;
	cin.getline(first_name, size);

	char last_name[size];
	cout << "What is your last name?";
	cin >> last_name;

	char grade;
	cout << "What letter grade do you deserve?";
	cin >> grade;

	int age;
	cout << "What is your age?";
	cin >> age;

	cout << endl;
	cout << "Name:" << last_name << "," << first_name << endl;
	cout << "Grade:" << char(grade + 1) << endl;
	cout << "Age:" << age << endl;

	system("pause");
	return 0;
}
#endif

2. 修改程序清单4.4,使用C++string类,而不是char数组。

//reading more than one word with getline
#if 1
#include
#include//要使用string类,必须在程序中包含头文件string。
using namespace std;

int main()
{
	//const int Arsize = 20;
	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 << " for you, " << name << "." << endl;
	system("pause");
	return 0;
}
#endif

3.

你可能感兴趣的:(学习,c++,开发语言,visualstudio,软件工程)