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

1598 views c++
4

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!

answered question

I would recommend learning how to use a debugger with the core file to find the error. If you are have gdb, you can do this as follows: "gdb MyProg core.<ProcId>". This will tell you exactly what line number the program is dying on.

yes, the aux uninitialized variable is one problem. Another is you are accessing the username_ and password_ strings beyond the length of the strings in setUnamep.

1 Answer

4

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

posted this

Have an answer?

JD

Please login first before posting an answer.