C++ PRIMER PLUS第六版课后编程答案4.1–4.5


 

4.1

 

#include <iostream>
void main41()
{
	using namespace std;
	cout<<"What is your first name?";
	char fname[20];
	cin.getline(fname,20);
	//int fsize=sizeof(fname);
	//cout<<b;
	//cout<<fname;
	cout<<endl<<"What is your last name ?";
	char lname[20];
	cin.getline(lname,20);
	cout<<endl<<"what letter grade do you deserve?";
	char grade;
	cin>>grade;
	cout<<"\nwhat is your age?";
	int age;
	cin>>age;
	cout<<endl<<"Name:"<<lname<<" ,"<<fname<<endl;
	cout<<"Grade:"<<char(grade+1);
	cout<<endl<<"Age:"<<age;
	cin.get();
	


}

4.2

#include <iostream>
#include <string>
using namespace std;
void main42()
{
	string name;
	cout<<"Enter your name:\n";
	//cin>>name;//一定要包含string头文件,否则会出错
	getline(cin,name);
	cout<<"Enter your favorite dessert:\n";
	string dessert;
	getline(cin,dessert);
	cout<<"I have some delicous "<<dessert;
	cout<<" for you ,"<<name<<endl;
	cin.get();


}

 

4.3

#include <iostream>
using namespace std;
void main43()
{
	const int ArrSize=20;
	char name[ArrSize];
	char xing[ArrSize/2];
	cout<<"Enter your first name:";
	cin.getline(name,20);
	cout<<endl<<"Enter your last name:";
	cin.get(xing,10);
	cout<<endl<<"Here's the information in a single string "
		<<xing<<","<<name;
	cin.get();






}

 

4.4

#include <iostream>
#include <string>

void main44()
{
	using namespace std;
	cout<<"Enter your First Name:";
	string fname;
	getline(cin,fname);
	cout<<"\nEnter your last name:";
	string lname;
	getline(cin,lname);
	cout<<endl<<"Here's the information in a single string:"
		<<lname<<", "<<fname;
	cin.get();
		
}

 

4.5

#include <iostream>
#include <string>
using namespace std;
struct CandyBar
{
	string name;
	double weight;
	int kaluli;
};
void main45()
{
	CandyBar snack={"OO CANDAY",2.5,60};
	cout<<"name:"<<snack.name;
	cout<<"\nweight:"<<snack.weight<<" \n kaluli: "<<snack.kaluli;
	cin.get();



}

 

——————————————————————————————————————————————————

//写的错误或者不好的地方请多多指导,可以在下面留言或者给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。

转载请注明出处:https://www.royalchen.com/

author:royalchen

Email:royalchen@royalchen.com

———————————————————————————————————————————————————

 

 


发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注