|
发表于 2020-11-26 11:10:00
|
显示全部楼层
有点夸张地说,除了int main 和 return 0 没有一句代码是无辜的
建议不要使用数组指针,用引用更不容易出错
- #include<iostream>
- #include<fstream>
- #include<string>
- using namespace std;
- const int num = 3;
- struct box {
- string marker;
- float height;
- float width;
- float length;
- float volume;
- };
- void display_box(box b[]);
- void set_box(box(*pb)[num]);
- int main() {
- box pa[num];
- //pa = new box[num];
- int i = 0;
- do {
- cout << "enter the name,person" << i + 1 << ":";
- getline(cin, pa[i].marker);
- //cin.get();
- cout << "enter the height:";
- cin >> pa[i].height;
- cin.get();
- cout << "enter the width:";
- cin >> pa[i].width;
- cin.get();
- cout << "enter the length:";
- cin >> pa[i].length;
- cin.get();
- } while (++i < num);
- display_box(pa);
- set_box(&pa);
- return 0;
- }
- void display_box(box b[]) {
- for (int i = 0; i < num; i++) {
- cout << "person" << i + 1 << ":" << endl;
- cout << "name:" << b[i].marker << endl;
- cout << "height:" << b[i].height << endl;
- cout << "width:" << b[i].width << endl;
- cin.get();
- }
- }
- void set_box(box(* pb)[num]) {
- for (int i = 0; i < num; i++) {
- cout << "person" << i + 1 << ":" << endl;
- (*pb)[i].volume = ((*pb)[i].height) * ((*pb)[i].length) * ((*pb)[i].width);
- cout << "volume:" << (*pb)[i].volume << endl;
- cin.get();
- }
- }
复制代码 |
|