cpp:处理字符串与数字的混合输入(动态数组)
来源:互联网 发布:工商局可以投诉淘宝吗 编辑:程序博客网 时间:2024/05/29 12:52
c++
中可以很方便的使用动态数组以及结构,也可以很方便的输入输出。不过,对于混合输入,特别是字符串与数字的混合输入的时候,还是需要一点注意。
对于c++
中数组,以及结构的数组也要留意一下,当前的变量对应的是指针,还是对象,或者是值?
下面是一个简单的案例,收集用户的车子厂商,以及生产年份,并输出相关收集的内容。
#include <iostream>#include <cstring>using namespace std;struct car { string company; int productiveYear;};int main() { int Amount; cout << "How many cars do you wish to catalog? "; cin >> Amount; cin.get(); // drop \n car *cars = new car[Amount]; const int TempLength = 80; char *tempInput = new char[TempLength]; for (int index = 0; index < Amount; ++index) { cout << "Car #" << (index + 1); cout << "Please enter the make:"; cin.getline(tempInput, TempLength); cars[index].company = tempInput; cout << "Please enter the your made:"; cin >> cars[index].productiveYear; cin.get(); // drop \n// cout << cars[index].company << "## " << tempInput; } delete[] tempInput; cout << "Here is your collection:" << endl; for (int i = 0; i < Amount; ++i) { cout << cars[i].productiveYear << " " << cars[i].company << endl; } return 0;}
输出如下:(粗体是输入)
How many cars do you wish to catalog? 2
Car #1
Please enter the make:比亚迪 秦
Please enter the your made:1962
Car #2
Please enter the make:保时捷 卡宴
Please enter the your made:1866
Here is your collection:
1962 比亚迪 秦
1866 保时捷 卡宴
注意几点:
cars
是一个数组;cars[0]
是一个结构体,还是一个结构指针呢?– 是结构体,不是指针。- 通过
typeinfo
头文件中的typeid(arg).name()
可以证实这一点。
- 通过
- 混合输入的时候,注意换行时
cin
的不同处理。
阅读全文
0 0
- cpp:处理字符串与数字的混合输入(动态数组)
- 数组的输入与输出.cpp
- C++中利用文件输入对象(即ifstream对象)来读取文本文件中带空格的字符串以及混合输入数字与字符串
- oracle中混合数字与字符的字符串排序
- 根据输入的字符串创建动态字符串数组
- [CPP]宏的字符串处理
- JavaScript字符串与数字的特殊处理
- 对于输入字符串数组的处理,三维数组
- 16-判断输入的是否是数字(字符串处理)
- C++ ifstream 从文件中 字符串和数字混合输入
- C++ ifstream 从文件中 字符串和数字混合输入
- 使用java Random动态传递位数 生成指定位数的随机字符串-数字字母混合
- 数字的字符串处理
- 数字的字符串处理
- 数字的字符串处理
- 数字的字符串处理
- 数字的字符串处理
- 中英文数字混合的复合格式处理
- 五子棋
- SSH与SSM学习之SSH实现CRM练习08——数据字典表
- 谈IT伦理与道德
- 软件测试与现在
- SSH与SSM学习之SSH实现CRM练习09——新增客户
- cpp:处理字符串与数字的混合输入(动态数组)
- Kickdown, ACM/ICPC NEERC 2006, UVa1588
- ES报错:Can not be imported as a dangling index as index with same name already exists in cluster me
- Java 基础夯实3:通过字节码了解内部类
- 垃圾收集器
- 《悟空传》绘本---by乱舞
- TM1638八位数码管八按键驱动程序(stm32f103驱动,使用了stm32 3.5库)
- Android 动画1--View控件的显示和隐藏效果
- 总结——STM32F103C8T6通过MAX31865读取PT100电阻值