简体   繁体   中英

Segmentation fault (core dumped) C++ Object Oriented Programming

I'm a student of system engineering, 2nd semester of the ULA (Universidad de los Andes)

So, I'm programming a c++ mini project for university. The project consists of making a draft of a software oriented to buying and selling crypto currencies, however, since yesterday I've been getting a problem with it (a segmentation fault core dumped specifically)... So, as this page had been helpful with my previous programs and this time I didn't find something that could have helped me, I decided to register and ask in case someone is willing to help me.

#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;
}

That's it, I would be very grateful if someone can help me solving this problem. Also, if you have a suggestion about another thing, It'll be a pleasure to read it!

There seems to be some trouble with this method

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;
}

The 'aux' variable is used before it is set resulting in undefined behavior.

Also the call is passing a 0 instead of a float[]. The compiler interprets the 0 as a nullptr, resulting a crash in ::setBuy

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

Probably some other issues but fixing these will be a step in the right direction

Your core dumped is in the setBuy function. You ask for an array of float, but when you call it in your code, you pass a "0", but you should pass an array of 20 elements.

The aux variable is set inside the function, but I think you should pass it from the signature of the function.

Also, the cantidad variable that you are using inside that function is not the one in the signature (you should remove it from the signature, or add an _ to cantidad).

I also looked into your setUnamep function, you should use an std::map for your username and password management (You can search for an already existing keys in log(n)).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM