**疫情期间学习C++
4-1
需要使用cin.get()设置读取位数,避免空格导致无法读取多个词

#include <iostream>
struct info_people //define structure
{char first_name[20];char last_name[20];char grade;unsigned int age;
};
int main()
{using namespace std;info_people* pd = new info_people;cout << "What is your first name?";cin.get(pd->first_name, 20);cout << endl << "What is your last name?";cin >> (*pd).last_name;cout << endl << "What letter grade do you deserve?";cin >> (*pd).grade;cout << endl << "What is your age?";cin >> pd->age;cout << endl << "Name: " << (*pd).first_name << ", " << (*pd).last_name << endl;cout << "Grade: " << char((*pd).grade + 1) << endl;cout << "Age: " << (*pd).age << endl;delete pd;return 0;
}

4-2
getline(cin,name)用于读取一行的string

#include <iostream>
#include <string>
int main()
{using namespace std;string name;string dessert;cout << "Enter your name:\n";getline(cin, name);cout << "Enter your favourite dessert:\n";getline(cin, dessert);cout << "I have some delicious " << dessert;cout << " for you, " << name << ".\n";return 0;
}

4-3
char设置长度,没空格可以直接cin

#include <iostream>
#include <cstring>
int main()
{using namespace std;const int size = 15;char first_name[size];char last_name[size];string dessert;cout << "Enter your fisrt name: ";cin >> first_name;cout << "\nEnter your last name: ";cin >> last_name;cout << "\nHere's the information in a single string: " << first_name << ", " << last_name << endl;return 0;
}

4-4
不含空格同样可以直接cin

#include <iostream>
#include <string>
int main()
{using namespace std;string first_name;string last_name;string dessert;cout << "Enter your fisrt name: ";cin >> first_name;cout << "\nEnter your last name: ";cin >> last_name;cout << "\nHere's the information in a single string: " << first_name << ", " << last_name << endl;return 0;
}

4-5

#include <iostream>
struct CandyBar
{char brand[20];float weight;unsigned int cal;
};int main()
{using namespace std;CandyBar snack ={"Mocha Munch", 2.3, 350};cout << "Brand: " << snack.brand << "\nWeight: " << snack.weight << "\nCal: " << snack.cal << endl;return 0;
}

4-6
不知道题目的意思理解错了没有。。。

#include <iostream>
struct CandyBar
{char brand[20];float weight;unsigned int cal;
};int main()
{using namespace std;CandyBar snack;cout << "Enter the brand: ";cin >> snack.brand;cout << "\nEnter the weight: ";cin >> snack.weight;cout << "\nEnter the cal: ";cin >> snack.cal;cout << "Brand: " << snack.brand << "\nWeight: " << snack.weight << "\nCal: " << snack.cal << endl;return 0;
}

4-7

#include <iostream>
#include <string>
struct Pissa_Info
{std::string brand;float diameter;float weight;
};int main()
{using namespace std;Pissa_Info pissa;cout << "Enter the brand: ";getline(cin, pissa.brand);cout << endl << "Enter the diameter: ";cin >> pissa.diameter;cout << endl << "Enter the weight: ";cin >> pissa.weight;cout << "Brand: " << pissa.brand << "\nDiameter: " << pissa.diameter << "\nWeight: " << pissa.weight<< endl;return 0;
}

4-8
cin数字之后,要使用getline的话需要先用cin.get()跳过换行符

#include <iostream>
#include <string>
using namespace std;struct Pissa_Info
{string brand;float diameter;float weight;
};int main()
{Pissa_Info* pd = new Pissa_Info;cout << "Enter the diameter: ";cin >> pd->diameter;cin.get();cout << endl << "Enter the brand: ";getline(cin, pd->brand);cout << endl << "Enter the weight: ";cin >> pd->weight;cout << "Brand: " << (*pd).brand << "\nDiameter: " << (*pd).diameter << "\nWeight: " << (*pd).weight<< endl;delete pd;return 0;
}

4-9

#include <iostream>
#include <string>
using namespace std;
struct CandyBar
{string brand;float weight;unsigned int cal;
};int main()
{CandyBar* pd = new CandyBar;cout << "Enter the brand: ";getline(cin, pd->brand);cout << "\nEnter the weight: ";cin >> pd->weight;cout << "\nEnter the cal: ";cin >> pd->cal;cout << "Brand: " << (*pd).brand << "\nWeight: " << (*pd).weight << "\nCal: " << (*pd).cal << endl;delete pd;return 0;
}

4-10

#include
#include

int main()
{
using namespace std;
array<float, 3> grade;
cout << "The first time finish 40m: ";
cin >> grade[0];
cout << "\nThe second time finish 40m: ";
cin >> grade[1];
cout << "\nThe third time finish 40m: ";
cin >> grade[2];
cout << "Average time is " << (grade[0] + grade[1] + grade[2]) / 3 << endl;
return 0;
}

C++PrimerPlus学习——第四章编程练习相关推荐

  1. C++PrimerPlus学习——第十七章编程练习

    17-1 不知道有没有理解错题意,参考list17.14 #include <iostream>int main() {using std::cout;using std::cin;usi ...

  2. C++PrimerPlus学习——第十三章编程练习

    13-1 注意char*前面加const,不然就会报错 Classis.h #ifndef CLASSIC_H_ #define CLASS_H_ #include <string> cl ...

  3. C++PrimerPlus学习——第十一章编程练习

    11-1 应该是修改list11.15,当当官方店买的,难道是盗版书吗... 打开file之后,操作跟cout类似 vect.h #ifndef VECT_h_ #define VECT_h_ #in ...

  4. C++PrimerPlus学习——第七章编程练习

    感觉变困难了很多,必须要注意细节,不如就会出各种bug 7-1 #include <iostream> double average(double a, double b);int mai ...

  5. C++PrimerPlus学习——第六章编程练习

    6-1 有个问题,如果输入的字符既不是数字也不是字母是不是应该原样输出呢? #include <iostream> #include <cctype>int main() {u ...

  6. STM32固件库(标准外设库)入门学习 第四章OLED屏幕使用

    STM32固件库(标准外设库)入门学习 第四章OLED屏幕使用 本学习教程,参考B站江科大自化协STM32视频,型号为STM32F103C8T6. 文章目录 STM32固件库(标准外设库)入门学习 第 ...

  7. c语言第六版第四章答案,C primer plus 第六版 第6版 004章 第四章 编程 练习 答案 中文...

    第四章 编程练习 1.编写一个程序,提示用户输入名和姓,然后以『名,姓』的格式打印出来. char name[40]; char family[40]; printf("请输入名和姓:\n& ...

  8. C++primer plus第四章编程练习自编程序

    //第四章编程练习 //eg.1显示信息 #include <iostream> using namespace std; const int SIZE = 20; int main() ...

  9. C Primer Plus学习_8第四章编程练习(略带解释 )

    第四章就这样结束了,下面我们来做书后习题. 如果你愿意练习,请不要看答案. 每个程序之后可能会有一些解释,都是我做的时候遇到的问题和是如何解决的,如果你还有其他疑问,评论区见,一起探讨. ------ ...

最新文章

  1. hash hashcode变化_hashmap重写key的hashcode问题
  2. 基于 OpenCV 的图像分割项目实战
  3. 《并行计算的编程模型》一2.4.1 GASNet段
  4. 计算机科学与技术年会,2017年全国理论计算机科学学术年会在武汉召开
  5. tensorflow中的关键字global_step使用
  6. linux内核申请内存的方法,Linux内核空间的内存申请常用函数
  7. 用Spring长轮询Tomcat
  8. git学习(9):git 添加 ssh keys 出现如下错误
  9. POJ1061 青蛙的约会【扩展欧几里得算法】
  10. C# 窗体全透明,控件不透明
  11. envi反演水质参数_基于大气校正法的Landsat 8 TIRS地表温度反演
  12. 【毕设狗】【单片机毕业设计】基于单片机的红外遥控器-实物设计
  13. 彩虹QQ查看对方ip原理
  14. POS收单地区代码表(2015年3月版)
  15. mess组网 中继_Mesh中继模式,哪个联网比较好?
  16. 全球太阳能、风能资源空间分布数据集
  17. android动画封装,Android开场动画类完整实现代码
  18. 鸿蒙第三代手机,原创 荣耀Magic 3最新确认,鸿蒙系统+双6400万,最期待的荣耀来了...
  19. ios-swift imgview 加载网络图片
  20. ML:从工程化思维分析—机器学习团队十大角色的简介(背景/职责/产出物):产品经理、项目经理、业务咨询顾问、数据科学家、ML研究员、数据工程师、ML工程师、DevOps/软件开发/交付工程师

热门文章

  1. HDU 5777 domino
  2. php5.3 延迟静态绑定 static关键字
  3. C#中IDisposable 回收非托管资源
  4. 转:数字对讲机常识介绍
  5. 100C之13:他该如何存款?
  6. malloc coredump(宕)的问题
  7. python实现路由功能_python 实现重启路由器
  8. cuda 编译 linux,Linux下安装Tensorflow源码及编译
  9. php 附近的距离,PHP查询附近的人及其距离的实现方法_PHP
  10. python中的wx_配置 Python的wxWidgets可视开发环境 | 学步园