有没有大佬帮我看看这是哪里出了问题,导致main函数中的for循环运行一次就退出程序运行了

代码如下

#include <iostream>
#include <string.h>
using namespace std;
class CRole {
private:
    char* pName;
    int iCash;
    int iSavings;
    char* pGoodWords;
    char* pBadWords;
    static int iRoleNum;
    static int iMoney;
    static int totalAssets;
    friend void input(CRole& role);
public:
    void Speak() {
        int total = iCash + iSavings;
        if (total > iMoney)
            cout << pGoodWords << endl;
        else
            cout << pBadWords << endl;
    }
    CRole(const char* pN, const char* pGood, const char* pBad) {
        pName = new char[20];
        strcpy_s(pName, 20, pN);
        pGoodWords = new char[100];
        strcpy_s(pGoodWords, 100, pGood);
        int len = strlen(pBad);
        pBadWords = new char[len + 1];
        strcpy_s(pBadWords, len + 1, pBad);
        cout << pName << "角色进入" << endl;
        iCash = iSavings = 0;
    }
    CRole(CRole& role) {
        pName = new char[20];
        strcpy_s(pName, 20, role.pName);
        pGoodWords = new char[100];
        strcpy_s(pGoodWords, 100, role.pGoodWords);
        pBadWords = new char[100];
        strcpy_s(pBadWords, 100, role.pBadWords);
        cout << pName << "角色进入" << endl;
        iRoleNum++;
        iCash = iSavings = 0;
    }
    ~CRole() {
        cout << pName << "角色退出" << endl;
        iRoleNum--;
    }
};
int CRole::iRoleNum = 0;
int CRole::iMoney = 0;
int CRole::totalAssets = 0;
void input(CRole& role)
{
    cout << "请输入金钱和存款" << endl;
    cin >> role.iCash >> role.iSavings;
    CRole::totalAssets += (role.iCash + role.iSavings);
    CRole::iMoney = CRole::totalAssets / CRole::iRoleNum;

}
int main()
{
    CRole roles[5] = {
      {"孙小美", "好的开始是成功的一半!","	还给人家啦"},
       {"钱夫人",	"你的是我的,我的还是我的!",	"老娘跟你没完!"},
      {"沙隆巴斯","阿拉真主,我赞美你!","人生不如意十有八九"},
      {"李四","",""},
      {"张三","",""} };
    for (int i = 0; i < 5; i++) {
        input(roles[i]);
        
    }
}

 

要调试一下,你可以考虑多写一些打印语句,锁定问题出现的地方,然后改进代码。

看看58行,被除数CRole::iRoleNum是0直接宕机了,检查一下你的逻辑。

您好,我是有问必答小助手,你的问题已经有小伙伴为您解答了问题,您看下是否解决了您的问题,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632

非常感谢您使用有问必答服务,为了后续更快速的帮您解决问题,现诚邀您参与有问必答体验反馈。您的建议将会运用到我们的产品优化中,希望能得到您的支持与协助!

速戳参与调研>>>https://t.csdnimg.cn/Kf0y