VC驿站

 找回密码
 加入驿站

QQ登录

只需一步,快速开始

搜索
查看: 496|回复: 2

程序编译不成功(小白):问题在哪里呢?

[复制链接]
54_avatar_middle
最佳答案
0 
在线会员 发表于 2020-11-24 11:02:07 | 显示全部楼层 |阅读模式
本帖最后由 suncz123 于 2020-11-24 22:17 编辑

#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[]);
void set_box(box*[]);

int main() {
        box *pa[num];
        //pa = new box[num];
        int i = 0;
        do {
                cout << "enter the name,person"<<i+1<<":";
                getline(cin,pa->marker);
                cin.get();
                cout << "enter the height:";
                cin >> pa->height;
                cin.get();
                cout << "enter the width:";
                cin >> pa->width;
                cin.get();
                cout << "enter the length:";
                cin >> pa->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.marker << endl;
                cout << "height:" << b.height << endl;
                cout << "width:" << b.width << endl;
                cin.get();
        }
}

void set_box(box* pb[]) {
        for (int i = 0; i < num ; i++) {
                cout << "person" << i +1<< ":" << endl;
                pb->volume = (pb->height) * (pb->length) * (pb->width);
                cout << "volume:" <<pb->volume<<endl;
                cin.get();
        }
}




上一篇:求解一段反汇编代码
下一篇:向导创建的MFC对话框项目,窗口右上角的关闭事件在哪里的
53_avatar_middle
最佳答案
0 
donate_vip 发表于 2020-11-26 01:26:46 | 显示全部楼层
开头的类对象的二维数组我真不懂,只能这样子啦
const int num = 3;
struct box {
        string marker;
        float height;
        float width;
        float length;
        float volume;
};

void display_box(box[]);
void set_box(box[]);

int main() {
       
        box pa[num];
        //pa = new box[num];
       
        int i = 0;
        do {
                cout << "enter the name,person" << i + 1 << ":";
                getline(cin, pa.marker);
                //cin.get();
                cout << "enter the height:";
                cin >> pa.height;
                cin.get();
                cout << "enter the width:";
                cin >> pa.width;
                cin.get();
                cout << "enter the length:";
                cin >> pa.length;
                cin.get();
               
                i++;
        } 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.marker << endl;
                cout << "height:" << b.height << endl;
                cout << "width:" << b.width << endl;
                cin.get();
        }
}

void set_box(box pb[]) {
        for (int i = 0; i < num; i++) {
                cout << "person" << i + 1 << ":" << endl;
                pb.volume = (pb.height) * (pb.length) * (pb.width);
                cout << "volume:" << pb->volume << endl;
                cin.get();
        }
}
开头的类对象的二维数组我真不懂,只能这样子啦
C:\Users\ken\Desktop\lkrzgba\QQ截图20201126012503.png
70_avatar_middle
最佳答案
49 
在线会员 发表于 2020-11-26 11:10:00 | 显示全部楼层
有点夸张地说,除了int main 和 return 0 没有一句代码是无辜的程序编译不成功(小白):问题在哪里呢?
建议不要使用数组指针,用引用更不容易出错
  1. #include<iostream>
  2. #include<fstream>
  3. #include<string>
  4. using namespace std;
  5. const int num = 3;
  6. struct box {
  7.     string marker;
  8.     float height;
  9.     float width;
  10.     float length;
  11.     float volume;
  12. };

  13. void display_box(box b[]);
  14. void set_box(box(*pb)[num]);

  15. int main() {
  16.     box pa[num];
  17.     //pa = new box[num];
  18.     int i = 0;
  19.     do {
  20.         cout << "enter the name,person" << i + 1 << ":";
  21.         getline(cin, pa[i].marker);
  22.         //cin.get();
  23.         cout << "enter the height:";
  24.         cin >> pa[i].height;
  25.         cin.get();
  26.         cout << "enter the width:";
  27.         cin >> pa[i].width;
  28.         cin.get();
  29.         cout << "enter the length:";
  30.         cin >> pa[i].length;
  31.         cin.get();
  32.     } while (++i < num);
  33.     display_box(pa);
  34.     set_box(&pa);
  35.     return 0;
  36. }

  37. void display_box(box b[]) {
  38.     for (int i = 0; i < num; i++) {
  39.         cout << "person" << i + 1 << ":" << endl;
  40.         cout << "name:" << b[i].marker << endl;
  41.         cout << "height:" << b[i].height << endl;
  42.         cout << "width:" << b[i].width << endl;
  43.         cin.get();
  44.     }
  45. }

  46. void set_box(box(* pb)[num]) {
  47.     for (int i = 0; i < num; i++) {
  48.         cout << "person" << i + 1 << ":" << endl;
  49.         (*pb)[i].volume = ((*pb)[i].height) * ((*pb)[i].length) * ((*pb)[i].width);
  50.         cout << "volume:" << (*pb)[i].volume << endl;
  51.         cin.get();
  52.     }
  53. }
复制代码
您需要登录后才可以回帖 登录 | 加入驿站 qq_login

本版积分规则

×【发帖 友情提示】
1、请回复有意义的内容,请勿恶意灌水;
2、纯数字、字母、表情等无意义的内容系统将自动删除;
3、若正常回复后帖子被自动删除,为系统误删的情况,请重新回复其他正常内容或等待管理员审核通过后会自动发布;
4、感谢您对VC驿站一如既往的支持,谢谢合作!

关闭

站长提醒上一条 /2 下一条

QQ|小黑屋|手机版|VC驿站 ( 辽ICP备09019393号-4 )|网站地图wx_jqr

GMT+8, 2023-12-3 17:47

Powered by CcTry.CoM

© 2009-2021 cctry.com

快速回复 返回顶部 返回列表