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<<" what 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: "; //cin>>name;//一定要包含string头文件,否则会出错 getline(cin,name); cout<<"Enter your favorite dessert: "; 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<<" Enter 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<<" weight:"<<snack.weight<<" kaluli: "<<snack.kaluli; cin.get(); }