c++ primer plus capture4 practice

目录

4.13.1information

4.13.2modify_instr2

4.13.3combine_name

 4.13.4combine_name_string

 4.13.5candy_bar

4.13.6candy_bar_array

4.13.7pizza

4.13.8pizza_new

4.13.9candy_bar_new

4.14.10array_grade_of_run


4.13.1information

//page 41,102

#include <iostream>
#include <cstring>

int main()
{
	using namespace std;
	char frist_name[20];
	string last_name;
	char grade_letter;
	unsigned int age;
	cout << "What is your first name?";
	cin.get(frist_name, 20).get();
    //cin.getline(frist_name,20);

	cout << "What is your last name?";
	cin >> last_name;

	cout << "What letter grade do you deserve?";
	cin >> grade_letter;
	//grade = grade + 1;

	cout << "What is your age?";
	cin >> age;

	cout << "Name: " << last_name << " , " << frist_name << endl;
	/*cout << "Grade: ";
	cout.put(grade_letter + 1);
	cout << endl;*/

	cout << "Grade: " << char(grade_letter+1) << endl;
	//cout << "Grade: " << grade << endl;
	cout << "Age: " << age << endl;
	return 0;
}

笔记:

①cin.get() and cin.getline()

cin.get(char*string——name,array size)会将换行符保留在输入序列,后期如果还使用cin.get()就会无法输入,所以保险起见加上cin.get()可调用下一字符。

cin.getline(name,size)会直接抛弃换行符

②output of char

cout<<grade_letter+1;

输出结果是char对于的int,计算机在计算的时候会将char转化成int,输出对于的数值

cout.put(grade_letter + 1)        //using cout.put() to display a char constant

cout << char(grade_letter + 1);        //输出的时候定义(grade_letter + 1) 为char

4.13.2modify_instr2

#include <iostream>
#include <string>
#include <cstring>

int main()
{
	using namespace std;
	string name;
	string dessert;

	cout << "Enter your name:\n";
	//cin.getline(name, ArSize);
	getline(cin,name);
	cout << "Enter your favorite dessert:\n";
	//cin.getline(dessert, ArSize);
	getline(cin, dessert);
	cout << "I have some delicious " << dessert;
	cout << " for you, " << name << ".\n";
	return 0;
}

string 会自己调整长度,无需像数组一样定义大小;

getline(cin,name)        cin在这里是声明

4.13.3combine_name

#include <iostream>
#include <cstring>

int main()
{
	using namespace std;
	char first_name[40];
	char last_name[40];
	char combined_name[80];
	cout << "Enter you first name: ";
	cin.get(first_name, 40).get();
	cout << "Enter you last name: ";
	cin.get(last_name, 40).get();
	strcpy_s(combined_name, last_name);
	strcat_s(combined_name, ", ");
	strcat_s(combined_name, first_name);
	cout << "Here's the information in a single string: " 
		<< combined_name << endl;
	return 0;
}

笔记:

在这里strcpy 和strcat操作的都是char数组

 4.13.4combine_name_string

#include <iostream>
#include <string>

int main()
{
	using namespace std;
	string first_name, last_name, combined_name;
	cout<< "Enter you first name: ";
	getline(cin, first_name);
	cout << "Enter you last name: ";
	getline(cin, last_name);
	combined_name = last_name + ", " + first_name;
	cout << "Here's the information in a single string: "
		<< combined_name << endl;
	return 0;
}

 4.13.5candy_bar

#include <iostream>
#include <cstring>

using namespace std;

struct CandyBar
{
	string branch_name;
	double weight;
	unsigned int calorie;
};
int main()
{
	CandyBar snack = { "Mocha Munch",2.3,350 };
	cout << "Branch name: " << snack.branch_name << endl;
	cout << "Weight: " << snack.weight << endl;
	cout << "Calorie: " << snack.calorie << endl;
	return 0;
}

4.13.6candy_bar_array

#include <iostream>
#include <string>

using namespace std;

struct candy_bar
{
	string branch_name;
	double weight;
	unsigned int calorie;
};
int main()
{
	candy_bar snack[3] =
	{
		{"Mocha Munch",2.3,350 },
		{"Cha Baidao",3.9, 400},
		{"Cache Cache",6.9, 300},
	};
	cout << snack[0].branch_name << "," << snack[0].calorie << "," << snack[0].weight << endl;
	cout << snack[1].branch_name << "," << snack[1].calorie << "," << snack[1].weight << endl;
	cout << snack[2].branch_name << "," << snack[2].calorie << "," << snack[2].weight << endl;

	return 0;
}

4.13.7pizza

#include <iostream>
#include <cstring>
#include <string>

struct pizza
{
	std::string name;
	double diameter;
	double weight;
};

int main()
{
	using namespace std;
	pizza company;
	cout << "Enter the name of pizza company: " << endl;
	getline(cin, company.name);
	cout << "Enter the diameter of the pizza: " << endl;
	cin>>company.diameter;
	cout << "Enter the weight of the pizza: " << endl;
	cin>> company.weight;

	cout << "Company name: " << company.name << endl;
	cout << "Diameter: " << company.diameter << endl;
	cout << "Weight: " << company.weight << endl;

	return 0;
}

4.13.8pizza_new

#include <iostream>
#include <cstring>
#include <string>

struct Pizza
{
	std::string name;
	double diameter;
	double weight;
};

int main()
{
	using namespace std;
	Pizza* pizza = new Pizza;

	cout << "Enter the diameter of the pizza: " << endl;
	cin >> pizza->diameter;

	cin.get();
    //输入diameter时的回车会影响下一个getline的使用

	cout << "Enter the name of pizza company: " << endl;
	getline(cin, pizza->name);
	cout << "Enter the weight of the pizza: " << endl;
	cin >> pizza->weight;

	cout << "Company name: " << pizza->name << endl;
	cout << "Diameter: " << pizza->diameter << endl;
	cout << "Weight: " << pizza->weight << endl;

	delete pizza;
	return 0;
}

注意直径输入完之后的回车需要cin.get()先读,getline才可以读取到下一个字符

4.13.9candy_bar_new

#include <iostream>
#include <string>

using namespace std;

struct candy_bar
{
	string branch_name;
	double weight;
	unsigned int calorie;
};
int main()
{
	candy_bar* snack = new candy_bar[3]{
		{"Cha Baidao",3.9,400},
		{"Mocha Munch",2.3,350 },
		{"Cache Cache",6.9, 300}
	};
	//candy_bar* snack = new candy_bar[3];
	//snack[0] = { "Mocha Munch",2.3,350 };
	//snack[1] = { "Cha Baidao",3.9, 400 };
	//snack[2] = { "Cache Cache",6.9, 300 };

	cout << snack[0].branch_name << "," << snack[0].calorie << "," << snack[0].weight << endl;
	cout << snack[1].branch_name << "," << snack[1].calorie << "," << snack[1].weight << endl;
	cout << snack[2].branch_name << "," << snack[2].calorie << "," << snack[2].weight << endl;

	delete[]snack;
	return 0;
}

    candy_bar* snack = new candy_bar[3];

type_name * pointer_name = new type_name [num_elements]

4.14.10array_grade_of_run

#include <iostream>
#include <array>

int main()
{
	using namespace std;
	array<double, 3>run_time;
	cout << "Please enter the result: ";
	cin >> run_time[0];
	cin >> run_time[1];
	cin >> run_time[2];
	cout << run_time[0] << " " << run_time[1] << " " << run_time[2] << endl;
	cout << "The average result is: " << (run_time[0] + run_time[1] + run_time[2]) / 3.0 << endl;
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值