c++ - 段错误(核心已转储)C++ 面向对象编程

标签 c++ c++11

我是 ULA (Universidad de los Andes) 第二学期系统工程专业的学生

所以,我正在为大学编写一个 C++ 迷你项目。该项目包括制作一个面向买卖加密货币的软件草案,但是,从昨天开始我就遇到了一个问题(专门转储了一个段错误核心)......所以,因为这个页面很有帮助对于我以前的程序,这次我没有找到可以帮助我的东西,我决定注册并询问是否有人愿意帮助我。

#include <iostream>
#include <string.h>
#include <stdlib.h>
using namespace std;

class usuario {
    private :
        string username[10], password[10];
        int aux;
    public :
        usuario();
        void setUnamep(string, string, int);
         string getUnamep();
        void setPass(string);
        string getPass();
        int DataAcc(string, string);
        ~usuario();
};

class moneda {  
    protected :
        float cantidad;
    public :
        moneda(float);
        void setCant(float);
        void getCant();
        ~moneda(); 
};

class bitcoin : public moneda {
    private :
        float btc[20];
    public :
        bitcoin (float);
        void setBuy(float, float[]);
        void getBuy();
        void mostrarc(float);
        ~bitcoin();
}; 


usuario::usuario () {
}

void usuario::setUnamep(string username_, string password_, int aux_) {
    string PreUser[20], aux_2;
    aux = aux_;
    for (int i= 1; i <= aux; i++) {  
        username[i] = username_[i];  
        password[i] = password_[i];
        cout<<"\nEnter an username: "; 
        cin>>username[i];               
        cout<<"Enter a password: ";  
        cin>>password[i];       
        username[0] = ".";      //pass 1 leer
            for (int v = 0 ; v < i; v++) {     
                if (username[v] == username[i]) {  
                    cout<<"\nUsername already in use. Choose another"<<endl;
                    username[i] = "null";
                    password[i] = "null";
                    i--;
                    v = 20000;
                }
            }
       }
}


int usuario::DataAcc(string InUs, string InPass) {
    bool ing = false, ret = false;
    int u = 0;
    do  {
        if (InUs==username[u] and InPass==password[u]) {
            ing = true;
            u = 10;
            ret = true;
        }
        else  //////
        u++;
    }
    while (ing == false and u<5);
    if (u == 5)
        cout<<"\nIncorrect user or password. Try again."<<endl;
    if (ing == true) {
        cout<<"\nAccount data..."<<endl;      
    }
    return ret;
}

usuario::~usuario() {
}

moneda::moneda(float cantidad_) {
    cantidad = cantidad_;
}

moneda::~moneda() {
}                                            

bitcoin::bitcoin(float cantidad_) : moneda(cantidad_) {
}

void bitcoin::setBuy(float cantidad_, float btc_[]) {
    int aux;
    for (int i = 0; i < 20 ; i++) {
        btc[i] = btc_[i];
    }
    cout<<"How many BTC do you wish to buy?: ";
    cin>>cantidad;
    btc[aux] = btc[aux] + cantidad;
}

bitcoin::~bitcoin() {   
} 

int main() {
    int opc = 0, aux1;
    string InUs, InPass;
    int aux2 = 0;
    bitcoin b1(0);
    cout<<"Welcome to BitZuela 2018, down there you have several options for you to choice which one do you want to run. ";
    cout<<"\n\n1. Sign Up."<<endl;
    cout<<"2. Log in."<<endl;
    cout<<"3. Finish program."<<endl;   
    usuario u1; 

    while (opc >=0 and opc <=2) {
    cout<<"\nPress the button of the option you want to run: "; 
    cin>>opc;

    if (opc==1) {
        cout<<"\nHow many accounts do you want to register?: ";
        cin>>aux1;
        u1.setUnamep("null", "null", aux1);
    } 
    if (opc==2) {
        cout<<"\nUsername: ";
        cin>>InUs;
        cout<<"Password: ";
        cin>>InPass; 
        aux2 = u1.DataAcc(InUs, InPass);
        if (aux2 == 1) {
            b1.setBuy(0,0);  //The problem is when this object is created
        }
    }
    if (opc == 3)
        cout<<"\nProgram finished."<<endl;
    }       
    return 0;
}

就是这样,如果有人能帮我解决这个问题,我将不胜感激。另外,如果您对其他事情有任何建议,很高兴阅读!

最佳答案

这个方法好像有点问题

void bitcoin::setBuy(float cantidad_, float btc_[]) {
    int aux;
    for (int i = 0; i < 20 ; i++) {
        btc[i] = btc_[i];
    }
    cout<<"How many BTC do you wish to buy?: ";
    cin>>cantidad;
    btc[aux] = btc[aux] + cantidad;
}

'aux' 变量在设置之前使用,导致未定义的行为。

此外,调用传递的是 0 而不是 float[]。编译器将 0 解释为 nullptr,导致::setBuy 崩溃

if (aux2 == 1) {
    b1.setBuy(0,0); 

可能还有一些其他问题,但解决这些问题将是朝着正确方向迈出的一步

关于c++ - 段错误(核心已转储)C++ 面向对象编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52359756/

相关文章:

c++ - 字符数组的初始值设定项字符串对于 char[] 而言太长

c++ - 哪个 QT 版本/构建更稳定?

linux - gcc 4.7.1 C++ 静态局部初始化挂起

c++ - 函数参数绑定(bind)的完美转发和二义性

c++ - 免费 C++11 IDE 的建议

c++ - 从 C++ 中的基类返回派生的 "this"的多态性

c++ - Infiniband 寻址 - 无 IBoIP 的主机名到 IB 地址

c++ - 如何正确地将带参数的函数传递给另一个函数?

c++ - 谷歌单元测试默认设置

c++ - 在匿名类中返回* this时, 'auto'返回类型是什么类型?