4.1
#include<stdafx.h>
#include <iostream>
#include<string>
using namespace std;
int main()
{
char f_name[10], l_name[10];
int age;
char grade;
cout << "what is your first name: ";
(cin.get(f_name,10)).get();
cout << "what is your last name? ";
cin.getline(l_name, 10);
cout << "what letter grade do you deserve? ";
cin >> grade;
grade = grade+1;//用于转换等级,不能直接用cout输出grade-1,将输出ASCII表示的十进制数
cout << "what is your age?";
cin >> age;
cout << "Name: " << l_name << ", " << f_name << endl;
cout << "Grade: " << grade << endl;
cout << "Age: " << age;
return 0;
}
注意:若使用string类,则应使用getline(cin,string),而不是cin.getline()函数;用于吸收get()函数中的换行符,否则将不能正常输入第二个字符;
使用string类型:
#include<stdafx.h>
#include <iostream>
#include<string>
using namespace std;
int main()
{
string f_name, l_name;
int age;
char grade;
cout << "what is your first name: ";
getline(cin, f_name);
cout << "what is your last name? ";
getline(cin, l_name);
cout << "what letter grade do you deserve? ";
cin >> grade;
grade = grade+1;//用于转换等级,不能直接用cout输出grade-1,将输出ASCII表示的十进制数
cout << "what is your age?";
cin >> age;
cout << "Name: " << l_name << ", " << f_name << endl;
cout << "Grade: " << grade << endl;
cout << "Age: " << age;
return 0;
}
4.2
#include<stdafx.h>
#include <iostream>
#include<string>
using namespace std;
int main()
{
string name, 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<< ".\n";
return 0;
}
4.3
#include<stdafx.h>
#include <iostream>
#include<string>
using namespace std;
int main()
{
char f_name[10], l_name[10];
cout << "enter your first name: ";
cin.getline(f_name, 10);
cout << "enter your last name: ";
cin.getline(l_name, 10);
cout << "Here's the information in a single string: " << l_name << ", " << f_name;
return 0;
}
4.4
#include<stdafx.h>
#include <iostream>
#include<string>
using namespace std;
int main()
{
string f_name, l_name;
cout << "enter your first name: ";
getline(cin,f_name);
cout << "enter your last name: ";
getline(cin,l_name);
cout << "Here's the information in a single string: " << l_name << ", " << f_name;
return 0;
}
4.5
#include<stdafx.h>
#include <iostream>
#include<string>
using namespace std;
struct CandyBar
{
string brand;
double weight;
int kal;
};
int main()
{
CandyBar snack = { "Mocha Munch",2.3,350 };
cout << snack.brand << endl << snack.weight << endl << snack.kal << endl;
return 0;
}
4.6
#include<stdafx.h>
#include <iostream>
#include<string>
using namespace std;
struct CandyBar
{
string brand;
double weight;
int kal;
};
int main()
{
CandyBar snack1 = { "Mocha Munch",2.3,350 };
CandyBar snack2 = { "mm",3.1,300 };
CandyBar snack3 = { "yangyang",3.5,450 };
cout << snack1.brand << ", " << snack1.weight << ", " << snack1.kal << endl;
cout << snack2.brand << ", " << snack2.weight << ", " << snack2.kal << endl;
cout << snack3.brand << ", " << snack3.weight << ", " << snack3.kal << endl;
return 0;
}
4.7
#include<stdafx.h>
#include <iostream>
#include<string>
using namespace std;
const int number = 2;
struct pizza
{
string brand;
double radius;
double weight;
};
int main()
{
pizza p[number];
int i;
for (i = 0; i < number; i++)
{
cout << "enter the brand of pizza" << i + 1<<"; ";
getline(cin, p[i].brand);
cout << "enter the radius of pizza" << i + 1 << "; ";
cin >> p[i].radius;
cout << "enter the weight of pizza" << i + 1 << "; ";
cin>>p[i].weight;
cin.get();//用于吸收最后一个cin的换行符,否则下次这个换行符将存为下一个pizza的品牌而无法输入品牌
}
for (i = 0; i < number; i++)
{
cout << "the information of the pizza" << i + 1 << " is: ";
cout << p[i].brand << " ," << p[i].radius << " ," << p[i].weight<<endl;
}
return 0;
}
4.8
#include<stdafx.h>
#include <iostream>
#include<string>
using namespace std;
struct pizza
{
string company;
double diameter;
double weight;
};
pizza * pz_in_Entetr()
{
pizza *p_in = new pizza;
cout << "enter the diameter of pizza: ";
cin>>p_in->diameter;
cin.get();
cout << "\nenter the company of pizza: ";
getline(cin, p_in->company);
cout << "\nenter the weight of pizza: ";
cin >> p_in->weight;
return p_in;//这里需要返回p_in指针,所以不能在函数中释放,即不能使用delete p_in,否则在main函数调用该函数后不能得到一个正确返回值;
}
int main()
{
pizza * pz_in_Entetr();
pizza *p;
p = pz_in_Entetr();
cout << p->company << endl << p->diameter << endl << p->weight;
delete p;//再使用完动态内存空间后释放空间
return 0;
}
4.9
#include<stdafx.h>
#include <iostream>
#include<string>
using namespace std;
struct CandyBar
{
string brand;
double weight;
int kal;
};
int main()
{
CandyBar *snack1 =new CandyBar { "Mocha Munch",2.3,350 };//分配内存空间的同时对其初始化,注意后面初始化的大括号前不能加=
CandyBar *snack2 =new CandyBar { "mm",3.1,300 };
CandyBar *snack3 =new CandyBar { "yangyang",3.5,450 };
cout << snack1->brand << ", " << snack1->weight<< ", " << snack1->kal << endl;
cout << snack2->brand << ", " << snack2->weight<< ", " << snack2->kal << endl;
cout << snack3->brand<< ", " << snack3->weight << ", " << snack3->kal << endl;
//cout << "size of *snack1: " << sizeof(*snack1) << endl;
//cout << "size of *snack2: " << sizeof(*snack2) << endl;
//cout << "size of *snack3: " << sizeof(*snack3) << endl;
//cout << "size of Candybar: " << sizeof(CandyBar) << endl;
//以上几行说明这样动态分配的内存空间与结构candybar一样,并不会根据每次输入的内容不同而改变所分配的内存空间的大小
delete snack1;
delete snack2;
delete snack3;
return 0;
}
4.10
#include<stdafx.h>
#include <iostream>
#include<array>
using namespace std;
int main()
{
array<double, 3> score;//定义一个名为score的包含三个double元素的数组
double aver;
cout << "enter score of first time in second:";
cin >> score[0];//yongarray定义的数组数组名的使用方法同直接定义的数组
cout << "enter score of second time in second:";
cin >> score[1];
cout << "enter score of third time in second:";
cin >> score[2];
aver = (score[0] + score[1] + score[2]) / 3.0;
cout << "now,the average of three times scores is : " << aver << endl;
return 0;
}