#include<iostream>usingnamespace std;struct Person
{char first_name[20];char last_name[20];char grade;int age;};intmain(){
Person x;
cout <<"What is your first name? ";
cin.getline(x.first_name,20);
cout <<"What is your last name? ";
cin.getline(x.last_name,20);
cout <<"What letter grade do you deserve?";
cin >> x.grade;
cout <<"What is your age?";
cin >> x.age;
cout <<"Name: "<< x.last_name <<", "<< x.first_name << endl;
cout <<"Grade: "<< x.grade << endl;
cout <<"Age: "<< x.age << endl;return0;}
#include<iostream>#include<string>usingnamespace std;intmain(){
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";return0;}
#include<iostream>#include<cstring>usingnamespace std;intmain(){char first_name[20];char last_name[20];
cout <<"Enter your first name: ";
cin.getline(first_name,20);
cout <<"Enter your last name: ";
cin.getline(last_name,20);char* result =strcat(strcat(last_name,", "), first_name);
cout <<"Here's the information in a single string: "<< result;return0;}
#include<iostream>#include<string>usingnamespace std;intmain(){
string first_name, last_name;
cout <<"Enter your first name: ";getline(cin, first_name);
cout <<"Enter your last name: ";getline(cin, last_name);
string result = last_name +", "+ first_name;
cout <<"Here's the information in a single string: "<< result;return0;}
#include<iostream>usingnamespace std;struct Pizza
{char company[20];double diameter, weight;};intmain(){
Pizza x;
cout <<"Enter the company of the pizza: ";
cin.getline(x.company,20);
cout <<"Enter the diameter of the pizza: ";
cin >> x.diameter;
cout <<"Enter the weight of the pizza: ";
cin >> x.weight;
cout <<"The company: "<< x.company <<"\nThe diameter: "<< x.diameter <<"\nThe weight: "<< x.weight;return0;}
#include<iostream>usingnamespace std;struct Pizza
{char company[20];double diameter, weight;};intmain(){
Pizza* x =new Pizza;
cout <<"Enter the diameter of the pizza: ";(cin >> x->diameter).get();//cin后使用getline,要吸收回车键
cout <<"Enter the company of the pizza: ";
cin.getline(x->company,20);
cout <<"Enter the weight of the pizza: ";
cin >> x->weight;
cout <<"The company: "<< x->company <<"\nThe diameter: "<< x->diameter <<"\nThe weight: "<< x->weight;delete x;return0;}
第四章 复合类型#include<iostream>using namespace std;struct Person{ char first_name[20]; char last_name[20]; char grade; int age;};int main(){ Person x; cout << "What is your first name? "; cin.getline(x.first_nam