第1题
#include<iostream>#include<string>usingnamespace std;struct student
{char firstname[40];char lastname[40];int age;char grade;};intmain(){
student std1;
cout <<"What your first name?";
cin.getline(std1.firstname,40);
cout <<"What your last name?";
cin.get(std1.lastname,40);
cin.get();
cout <<"What letter grade do you deserve?";
cin >> std1.grade;
cout <<"What is your age?";
cin >> std1.age;
cout <<"Name: "<< std1.lastname <<","<< std1.firstname << endl;
cout <<"Grade: "<< std1.grade << endl;
cout <<"Age: "<< std1.age << endl;return0;}
第2题
#include<iostream>#include<string>intmain(){usingnamespace std;
string name;
string dessert;
cout <<"Enter your name :\n";getline(cin, name);//cin 确定结尾的方式类似scanf(以空白作为读取结结束)因此cin只能一次性读取一个单词
cout <<"Enter your favorite dessert:\n";getline(cin, dessert);
cout <<"I have some delicious "<< dessert;
cout <<" for you, "<< name <<".\n";return0;}
第3题
#include<cstring>#include<iostream>intmain(){usingnamespace std;char firstname[50];char lastname[25];char sign[]=", ";
cout <<"Enter your first name:";
cin >> firstname;
cout <<"Enter your last name:";
cin >> lastname;strcat(firstname, sign);strcat(firstname, lastname);
cout <<"Here's the information in a single string: "<< firstname << endl;return0;}
第4题
#include<cstring>#include<iostream>#include<string>intmain(){usingnamespace std;
string firname;
string lstname;
string sign =", ";
cout <<"Enter your first name:";
cin >> firname;
cout <<"Enter your last name:";
cin >> lstname;
firname = firname + sign + lstname;
cout <<"Here's the information in a single string: "<< firname << endl;return0;}