1 void ch4_1() {
2 using namespace std;
3 string fname, lname;
4 char grade;
5 unsigned int age;
6 cout << "enter first name: ";
7 getline(cin, fname);
8 cout << "enter last name: ";
9 getline(cin, lname);
10 cout << "enter your grade: ";
11 cin >> grade;
12 cout << "enter your age: ";
13 cin >> age;
14 cout << "Name: " << lname << ", " << fname << endl;
15 cout << "Grade: " << grade + 1 << endl;
16 cout << "Age: " << age << endl;
17 }
18
19 void ch4_2() {
20 using namespace std;
21 string name, dessert;
22 cout << "Enter your name: ";
23 getline(cin, name);
24 cout << "Enter your favorite dessert: ";
25 getline(cin, dessert);
26 cout << "I have some delicious " << dessert << " for you, " << name << "." << endl;
27 }
28
29 void ch4_3() {
30 using namespace std;
31 char fname[20], lname[20], combine[41];
32 cout << "enter your first name: ";
33 cin.getline(fname, 20);
34 cout << "enter your last name: ";
35 cin.getline(lname, 20);
36 strcpy(combine, lname);
37 strcat(combine, ", ");
38 strcat(combine, fname);
39 cout << "Here's the information in a single string: " << combine << endl;
40 }
41
42 void ch4_4() {
43 using namespace std;
44 string fname, lname, combine;
45 cout << "enter your first name: ";
46 getline(cin, fname);
47 cout << "enter your last name: ";
48 getline(cin, lname);
49 combine = lname + ", " + fname;
50 cout << "Here's the information in a single string: " << combine << endl;
51 }
52
53 void ch4_5() {
54 using namespace std;
55 struct CandyBar{
56 string brand;
57 double weight;
58 unsigned int kll;
59 };
60 CandyBar snack = {"Mocha Munch", 2.3, 350};
61 cout << "brand: " << snack.brand << endl
62 << "weight: " << snack.weight << endl
63 << "kll: " << snack.kll << endl;
64 }
65
66 void ch4_6() {
67 using namespace std;
68 struct CandyBar{
69 string brand;
70 double weight;
71 unsigned int kll;
72 };
73 CandyBar snack;
74 cout << "enter brand: ";
75 getline(cin, snack.brand);
76 cout << "enter weight: ";
77 cin >> snack.weight;
78 cout << "enter kll: ";
79 cin >> snack.kll;
80 cout << "brand: " << snack.brand << endl
81 << "weight: " << snack.weight << endl
82 << "kll: " << snack.kll << endl;
83 }
84
85 void ch4_7() {
86 using namespace std;
87 struct Pizza{
88 string brand;
89 double diameter;
90 double weight;
91 };
92 Pizza pizza;
93 cout << "enter brand: ";
94 getline(cin, pizza.brand);
95 cout << "enter weight: ";
96 cin >> pizza.weight;
97 cout << "enter diameter: ";
98 cin >> pizza.diameter;
99 cout << "brand: " << pizza.brand << endl
100 << "weight: " << pizza.weight << endl
101 << "diameter: " << pizza.diameter << endl;
102 }
103
104 void ch4_8() {
105 using namespace std;
106 struct Pizza{
107 string brand;
108 double diameter;
109 double weight;
110 };
111 Pizza * p_pizza = new Pizza;
112 cout << "enter diameter: ";
113 cin >> p_pizza->diameter; cin.get();
114 cout << "enter brand: ";
115 getline(cin, p_pizza->brand);
116 cout << "enter weight: ";
117 cin >> p_pizza->weight;
118 cout << "diameter: " << p_pizza->diameter << endl
119 << "brand: " << p_pizza->brand << endl
120 << "weight: " << p_pizza->weight << endl;
121 }
122
123 void ch4_9() {
124 using namespace std;
125 struct CandyBar{
126 string brand;
127 double weight;
128 unsigned int kll;
129 };
130 CandyBar * snack_arr = new CandyBar[3];
131 snack_arr[0] = {"abc a", 34, 654};
132 snack_arr[1] = {"abc b", 34580, 4363};
133 snack_arr[2] = {"abc c", 756, 67};
134 for (int i = 0; i < 3; ++ i)
135 cout << "brand: " << snack_arr[i].brand << endl
136 << "weight: " << snack_arr[i].weight << endl
137 << "kll: " << snack_arr[i].kll << endl;
138 }
139
140 void ch4_10() {
141 using namespace std;
142 array<double, 3> race;
143 for (int i = 0; i < 3; ++ i){
144 cout << "race: ";
145 cin >> race[i];
146 }
147 for (int i = 0; i < 3; ++ i)
148 cout << "#" << i + 1 << ": " << race[i] << endl;
149 }

欢迎交流

【C++ Primer Plus】编程练习答案——第4章的更多相关文章

  1. 【C++ Primer Plus】编程练习答案——第12章

    1 // chapter12_1_cow.h 2 3 4 #ifndef LEARN_CPP_CHAPTER12_1_COW_H 5 #define LEARN_CPP_CHAPTER12_1_COW ...

  2. 【C++ Primer Plus】编程练习答案——第11章 (待更新)

    最近开学,事情较多,过两天更新...

  3. 【C++ Primer Plus】编程练习答案——第10章

    1 // chapter10_1_account.h 2 3 #ifndef LEARN_CPP_CHAPTER10_1_ACCOUNT_H 4 #define LEARN_CPP_CHAPTER10 ...

  4. 【C++ Primer Plus】编程练习答案——第9章

    1 // chapter09_golf.h 2 3 #ifndef LEARN_CPP_CHAPTER09_GOLF_H 4 #define LEARN_CPP_CHAPTER09_GOLF_H 5 ...

  5. 【C++ Primer Plus】编程练习答案——第8章

    1 void ch8_1_print(const std::string & str, int n = 0 ) { 2 using namespace std; 3 static int fl ...

  6. 【C++ Primer Plus】编程练习答案——第7章

    1 double ch7_1_harmonicaverage(double a, double b) { 2 return 2 / (1 / a + 1 / b); 3 } 4 5 void ch7_ ...

  7. 【C++ Primer Plus】编程练习答案——第6章

    1 void ch6_1() { 2 using namespace std; 3 char ch; 4 while ((ch = cin.get()) != '@') { 5 if (isdigit ...

  8. 【C++ Primer Plus】编程练习答案——第5章

    1 void ch5_1() { 2 using namespace std; 3 int small, big, sum{0}; 4 cout << "enter small ...

  9. 【C++ Primer Plus】编程练习答案——第3章

    1 void ch3_1() { 2 using namespace std; 3 unsigned int factor = 12; 4 unsigned int inch, feet; 5 cou ...

随机推荐

  1. TortoiseGit冲突和解决冲突

    产生冲突原因 产生:多个开发者同时使用或者操作git中的同一个文件,最后在依次提交commit和推送push的时候,第一个操作的是可以正常提交的,而之后的开发者想要执行pull(拉)和pull(推)操 ...

  2. SpringBoot获取所有接口的路由

    @Autowired WebApplicationContext applicationContext; @RequestMapping(value = "v1/getAllUrl" ...

  3. [ASP.NET MVC]@RenderSection,@RenderBody(),@RenderPage

    1.@RenderBody()  作用和母版页中的服务器控件类似,当创建基于此布局页面的视图时,视图的内容会和布局页面合并,而新创建视图的内容会通过布局页面的@RenderBody()方法呈现在标签之 ...

  4. FXGL游戏开发-JavaFX游戏框架

    FXGL 是一个JavaFX 游戏开发的框架,这个框架有两个版本,其中基于JDK1.8的版本已经不再维护,目前最新的是基于JDK11的版本,也就是Openjfx的版本. FXGL 提供了各种游戏范例: ...

  5. Elasticsearch集群搭建教程及生产环境配置

    Elasticsearch 是一个极其强大的搜索和分析引擎,其强大的部分在于能够对其进行扩展以获得更好的性能和稳定性. 本教程将提供有关如何设置 Elasticsearch 集群的一些信息,并将添加一 ...

  6. Confluence7.4安装并破解汉化教程

    Confluence是一款由JAVA编写用于企业知识库管理协同软件,多用于构建企业内部WIKI,支持多人协作,共享信息等. 当前系统环境Centos7.9,内存至少2G以上,数据库采用MySQL5.7 ...

  7. grpc基础

    RPC 框架原理 RPC 框架的目标就是让远程服务调用更加简单.透明,RPC 框架负责屏蔽底层的传输方式(TCP 或者 UDP).序列化方式(XML/Json/ 二进制)和通信细节.服务调用者可以像调 ...

  8. sublime text 的 Ctrl + P「模糊搜索算法」

    Reverse Engineering Sublime Text's Fuzzy Match 这是我能 google 到的最早的一篇关于 Sublime Text 的模糊搜索的文章. https:// ...

  9. 使用kubeadm安装kubernetes 1.21

    文章原文 配置要求 至少2台 2核4G 的服务器 本文档中,CPU必须为 x86架构 CentOS 7.8 或 CentOS Stream 8 安装后的软件版本为 Kubernetes v1.21.x ...

  10. Java - 注释、标识符、关键字

    背景 要开始磕 Java 了,虽然以前学过用过,但是差不多忘光光了... 现在直接搬狂神的视频素材,不再自己总结,要学的东西太多了... 注释 单行注释 // 多行注释 /* */ 文档注释 /** ...