LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-10-2020, 06:35 AM   #1
rahulvishwakarma
Member
 
Registered: Aug 2010
Posts: 138

Rep: Reputation: 2
Question segmentation fault in mysql_query in C++


hi to all, I am building a small project in MYSQL server and c++ as client in two VMs.
here is code where segmentation fault occures.( //segmentation error is commented out here)

Code:
void classbilling::billing()
{
    clrscr();
    drawrect();


    mysql = Conn::connection();

    char yesno;

    int lines;
    int billno;
    int amount;
    int instock;
    int numrowsaffected;
    int stock;
    std::string strquantity;
    long memno;
    long cardno;

    long memcardno;

    std::string strcardno;
    std::string stramxid;
    std::string strbillno;
    std::string strmemno;
    std::string strstock;

    std::string strrate;
    std::string strtotal;
    std::string strmaxid;

    float less = 1.0;

    MYSQL_RES *res;
    MYSQL_ROW row;

    gotoxy(10, 6);
    cout << "Are you a member : ";
    cin >> yesno;
    cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    if(toupper(yesno) == 'Y')
    {
        gotoxy(10, 7);
        cout << "Enter Membership Number : ";
        std::string strmemno;
        getline(cin, strmemno);
        int m = Conn::intvalidity(strmemno);
        memno = m;

        if(m == 0)
        {
            draw->gotoxy(10,10);
            cout << "Enter Valid Membership number";
            cin.get();
            return;
        }

        sql = "select name from tableMemberRecords where membershipno = '"+strmemno+"';";
        qstate = mysql_query(mysql, sql.c_str());
        if(!qstate)
        {
            res = mysql_store_result(mysql);
            if((row = mysql_fetch_row(res)) != nullptr)
            {
                if(row[0] == nullptr)
                {
                    gotoxy(10, 10);
                    cout << "Invalid Membership number";
                    cin.get();
                }
            }
        }

        gotoxy(10, 8);
        cout << "Enter Card Number : ";
        std::string strcardno;
        getline(cin, strcardno);
        m = Conn::intvalidity(strcardno);
        if(m == 0)
        {
            draw->gotoxy(10,10);
            cout << "Enter Valid card number";
            cin.get();
            return;
        }

        //display of member
        gotoxy(1, 12);
        cout << "before memsearch ";
        cin.get();

        mem1 = mem2->memberSearch(memno );
        winsize w;
        ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
        cardno = m;
        if(mem1->getmemcardno() != cardno)
        {
            gotoxy(10, 9);
            cout << "Incorrect card number";
            cin.get();
            return;
        }
        gotoxy(1, 14);
        cout << "after if mem1";
        cin.get();
        //strmemno = std::to_string(memno);
        cin.get();
        cout << "after to_string strmemeno : " << strmemno;
        cin.get();
//        mysql_free_result(res);
        sql = "select CardNo, AmountDeposited from tableMemberRecords where MembershipNo = '"+strmemno+"';";
        cout << "after sql : " << sql;
        cin.get();
        qstate = mysql_query(mysql, sql.c_str()); //segmentation error
        gotoxy(1, 15);
        cout << "after sql";
        cin.get();
        if(!qstate)
        {
            res = mysql_store_result(mysql);
            if((row = mysql_fetch_row(res))!= nullptr)
            {
                memcardno = atol(row[0]);
                amount = atoi(row[1]);
//                gotoxy(1, 14);
            }
        }
        else
        {
            gotoxy(1, 18);
            cout << "Error in else of select cardno : " << mysql_error(mysql);
            cin.get();
        }

        if(cardno != memcardno)
        {
            gotoxy(10, 14);
            cout << "incorrect  card number or membership number";
            cin.get();
            cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            return;
        }

        else if(mem1->getmemamount() > 100)
        {
            less = 0.95f;
            amount -= 5;
            mem1->setmemamount(amount);
        }
        else
        {
            gotoxy(10, 13);
            cout<< "Amount less than 100 ";
            gotoxy(10, 14);
            cout << "Continue with normal billing ";
            cin >> yesno;
            cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            if(toupper(yesno) == 'N')
            {
                return;
            }
        }
        gotoxy(1, 14);
        cout << "before to_string";
        cin.get();
        string stramt = std::to_string(amount);
        strcardno = std::to_string(cardno);
        strmemno = std::to_string(memno);

    gotoxy(1, 15);
    cout << "before update sql";
    cin.get();

        sql = "update tableMemberRecords set amountdeposited = '"+stramt+"' where membershipno = '"+strmemno+"';";

        qstate = mysql_query(mysql, sql.c_str());
        if(!qstate)
        {
            gotoxy(2, 16);
            cout << "Member Record is updated and updated record is : ";
            gotoxy(1, 17);
            mem2->memberSearch(memno);//, 0, 15);
            std::cin.get();
         }
    }

    else
    {
        memno = 0;
    }

    sql = "select max(Billno) from tableBilling";
    qstate = mysql_query(mysql, sql.c_str());
    if(!qstate)
    {
        res = mysql_store_result(mysql);
        if((row = mysql_fetch_row(res)) != nullptr)
        {
            if(row[0] == nullptr)
            {
                billno = 0;
            }
            else
            {
                billno = atoi(row[0]);
            }
            strbillno = std::to_string(++billno);
        }
    }

    product *prd = new product;

LABEL1:
        clrscr();
        drawrect();

        sql = "select p.productname, p.rate, quantity, total from tableProductRecords as p, tableBilling as b where billno = '"+
                strbillno+"' and p.productname in (select productname from tableProductRecords where productid =  b.productid);";

    qstate = mysql_query(mysql, sql.c_str());

    if(!qstate)
    {
        gotoxy(1, 13);
        res = mysql_store_result(mysql);

        sqlprocess *sqlp = new sqlprocess;

        numrowsaffected = sqlp->process_result_set(mysql, res);
    }
    productid = prd->displayProduct("PRODUCT DISPLAY");
    if(productid == 0)
    {
        draw->gotoxy(10, 10);
        cout << "Incorrect Product id";
        cin.get();
//        return;
        goto LABEL1;
    }

    if(productid > 0)
    {
        clrscr();
        drawrect();
        getcmenu->printMenu(salesmenu, "Billing");
        gotoxy(45, 3);
        cout << "Bill No. : " << billno;

        gotoxy(31, 5);
        cout << productid;

        string strpid = std::to_string(productid);
        sql = "select productname, stock, rate from tableProductRecords where productid = '"+strpid+"';";
        qstate = mysql_query(mysql,sql.c_str());
        if(!qstate)
        {
            res = mysql_store_result(mysql);

            if((row = mysql_fetch_row(res)) != nullptr)
            {
                gotoxy(31, 6);
                productname = row[0];
                cout << productname;

                gotoxy(31, 7);
                stock = atoi(row[1]);
                cout << stock;

                gotoxy(31, 8);
                rate = atoi(row[2]);
                cout << rate;
            }
            mysql_free_result(res);
        }

        sql = "select p.productname, p.rate, quantity, total from tableProductRecords as p, tableBilling as b where billno = '"+
                strbillno+"' and productname in (select productname from tableProductRecords where productid =  b.productid);";
        qstate = mysql_query(mysql, sql.c_str());

        if(!qstate)
        {
            gotoxy(1,13);
            res = mysql_store_result(mysql);
            sqlprocess *sqlp = new sqlprocess;
            lines = sqlp->process_result_set(mysql, res);
        }
        do
        {
            gotoxy(31, 9);
            winsize w;
            ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
            for(int x = 31; x <= w.ws_col-1; x++)
            {
                cout << " ";
            }
            gotoxy(31, 9);
            getline(cin, strquantity);
            quantity = Conn::intvalidity(strquantity);

            if(quantity == 0)
            {
                gotoxy(10, 10);
                cout << "Invalid Quantity";
                cin.get();
                gotoxy(31, 9);
            }
        }while(quantity == 0);

        gotoxy(15, 10);
        cout << "Total :         ";

        int qty = stock - quantity;
        if(qty >= 0)
        {
            strqty = std::to_string(qty);
            total = quantity * rate;
            gtotal += total;
            gotoxy(31, 10);
            cout << total;
        }

        else
        {
            gotoxy(10, 12);
            cout << "insufficient stock, available only " << stock;
            cin.get();
            goto LABEL1;
        }

        strstock = std::to_string(stock);
        strquantity = std::to_string(quantity);
        strrate  = std::to_string(rate);
        strtotal = std::to_string(total);

        date *dt = new date;
        time_t now = time(0);
        tm *ltm = std::localtime(&now);
        dt->yy = ltm->tm_year;
        dt->mm = ltm->tm_mon;
        dt->dd = ltm->tm_mday;

        string strdate = std::to_string(1900 + dt->yy) + "/" + std::to_string(dt->mm) + "/" + std::to_string(dt->dd);
        sql = "select max(id) from tableBilling;";
        qstate = mysql_query(mysql, sql.c_str());
        int maxid;
        if(!qstate)
        {
            res = mysql_store_result(mysql);
            if((row = mysql_fetch_row(res)) != nullptr)
            {
                if(row[0] == nullptr)
                {
                    maxid = 0;
                }
                else
                {
                    maxid = atoi(row[0]);
                }
                ++maxid;
                strmaxid = std::to_string(maxid);
            }
            mysql_free_result(res);
        }
        strmemno = std::to_string(memno);

        sql = "insert into tableBilling values ('"+strmaxid+"','"+strpid+"','"+strquantity+"','"+
                                                strtotal+"', '"+strbillno+"','"+strmemno+"','"+strdate+"');";

        qstate = mysql_query(mysql, sql.c_str());
        if(!qstate)
        {
            gotoxy(11, 11);
            cout << "Billing data inserted ";
            sql= "update tableProductRecords as p set p.stock = "+strqty+" where p.productid = "+strpid+";";
            qstate = mysql_query(mysql, sql.c_str());
            if(!qstate)
            {
                gotoxy(10, 12);
                cout << "stock data updated successfully";
                gotoxy(31, 7);
                cout << strqty;

                cin.get();

            }
            else
            {
                gotoxy(10, 12);
                cout << "error in billing data inserted " << mysql_error(mysql);
                cin.get();
            }

        }
        else
        {
            gotoxy(10, 11);
            cout << "Error in insert : " << mysql_error(mysql);
            cin.get();
            return;
        }

        sql = "select p.productname, p.rate, quantity, total from tableProductRecords as p, tableBilling as b where billno = '"+
                strbillno+"' and productname in (select productname from tableProductRecords where productid =  b.productid);";

        qstate = mysql_query(mysql, sql.c_str());

        if(!qstate)
        {
            gotoxy(1, 13);
            res = mysql_store_result(mysql);
            sqlprocess *sqlp = new sqlprocess;

            lines = sqlp->process_result_set(mysql, res);
        }
        else
        {
            gotoxy(1, 13);
            cout << "error in select billing " << mysql_error(mysql);
        }

        gotoxy(10, 12);
        winsize w;
        ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
        for(int i = 10; i < w.ws_col -1; i++)
        {
            cout << " ";
        }

        gotoxy(31, 17 + lines);
        cout << "Grand Total : " << gtotal;

        gotoxy(11,12);
        cout << "want to add more records : ";
        cin >> yesno;
        cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');



        if(toupper(yesno) == 'Y')
        {

            goto LABEL1;
        }

        if(less != 1)
        {
            float discount = 0.05 * gtotal;
            gotoxy(31, 18 + lines);
            cout << "Discount Rs. : " << discount;
            gotoxy(31, 19 + lines);
            cout << "Net Total : " << less * gtotal;
        }
    }

    mysql_free_result(res);
    mysql_close(mysql);

//    delete con;
    delete draw;
    delete mysql;
    delete res;
}
i don't know why this error is occuring. Please sugest me.
 
Old 12-10-2020, 07:06 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
Hi

This looks strange:

Code:
 qstate = mysql_query(mysql, sql.c_str());
        if(!qstate)
        {
            res = mysql_store_result(mysql);
Like most C functions, mysql_query returns 0 on success. If it fails, the value of res is never set. Then using mysql_free_result(res) will crash.

You should check the return value. If not zero, display an error message, and exit. And you can use mysql_errno or mysql_error to print an error message.
 
Old 12-10-2020, 07:33 AM   #3
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
Also there's a trailing ; in your sql. That's probably the reason it fails.

https://dev.mysql.com/doc/c-api/8.0/en/mysql-query.html
 
1 members found this post helpful.
Old 12-10-2020, 08:25 AM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,659

Rep: Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970
Quote:
Originally Posted by rahulvishwakarma View Post
hi to all, I am building a small project in MYSQL server and c++ as client in two VMs.
here is code where segmentation fault occures.( //segmentation error is commented out here)
Code:
void classbilling::billing()
{
    clrscr();
    drawrect();


    mysql = Conn::connection();

    char yesno;

    int lines;
    int billno;
    int amount;
    int instock;
    int numrowsaffected;
    int stock;
    std::string strquantity;
    long memno;
    long cardno;

    long memcardno;

    std::string strcardno;
    std::string stramxid;
    std::string strbillno;
    std::string strmemno;
    std::string strstock;

    std::string strrate;
    std::string strtotal;
    std::string strmaxid;

    float less = 1.0;

    MYSQL_RES *res;
    MYSQL_ROW row;

    gotoxy(10, 6);
    cout << "Are you a member : ";
    cin >> yesno;
    cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    if(toupper(yesno) == 'Y')
    {
        gotoxy(10, 7);
        cout << "Enter Membership Number : ";
        std::string strmemno;
        getline(cin, strmemno);
        int m = Conn::intvalidity(strmemno);
        memno = m;

        if(m == 0)
        {
            draw->gotoxy(10,10);
            cout << "Enter Valid Membership number";
            cin.get();
            return;
        }

        sql = "select name from tableMemberRecords where membershipno = '"+strmemno+"';";
        qstate = mysql_query(mysql, sql.c_str());
        if(!qstate)
        {
            res = mysql_store_result(mysql);
            if((row = mysql_fetch_row(res)) != nullptr)
            {
                if(row[0] == nullptr)
                {
                    gotoxy(10, 10);
                    cout << "Invalid Membership number";
                    cin.get();
                }
            }
        }

        gotoxy(10, 8);
        cout << "Enter Card Number : ";
        std::string strcardno;
        getline(cin, strcardno);
        m = Conn::intvalidity(strcardno);
        if(m == 0)
        {
            draw->gotoxy(10,10);
            cout << "Enter Valid card number";
            cin.get();
            return;
        }

        //display of member
        gotoxy(1, 12);
        cout << "before memsearch ";
        cin.get();

        mem1 = mem2->memberSearch(memno );
        winsize w;
        ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
        cardno = m;
        if(mem1->getmemcardno() != cardno)
        {
            gotoxy(10, 9);
            cout << "Incorrect card number";
            cin.get();
            return;
        }
        gotoxy(1, 14);
        cout << "after if mem1";
        cin.get();
        //strmemno = std::to_string(memno);
        cin.get();
        cout << "after to_string strmemeno : " << strmemno;
        cin.get();
//        mysql_free_result(res);
        sql = "select CardNo, AmountDeposited from tableMemberRecords where MembershipNo = '"+strmemno+"';";
        cout << "after sql : " << sql;
        cin.get();
        qstate = mysql_query(mysql, sql.c_str()); //segmentation error
        gotoxy(1, 15);
        cout << "after sql";
        cin.get();
        if(!qstate)
        {
            res = mysql_store_result(mysql);
            if((row = mysql_fetch_row(res))!= nullptr)
            {
                memcardno = atol(row[0]);
                amount = atoi(row[1]);
//                gotoxy(1, 14);
            }
        }
        else
        {
            gotoxy(1, 18);
            cout << "Error in else of select cardno : " << mysql_error(mysql);
            cin.get();
        }

        if(cardno != memcardno)
        {
            gotoxy(10, 14);
            cout << "incorrect  card number or membership number";
            cin.get();
            cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            return;
        }

        else if(mem1->getmemamount() > 100)
        {
            less = 0.95f;
            amount -= 5;
            mem1->setmemamount(amount);
        }
        else
        {
            gotoxy(10, 13);
            cout<< "Amount less than 100 ";
            gotoxy(10, 14);
            cout << "Continue with normal billing ";
            cin >> yesno;
            cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            if(toupper(yesno) == 'N')
            {
                return;
            }
        }
        gotoxy(1, 14);
        cout << "before to_string";
        cin.get();
        string stramt = std::to_string(amount);
        strcardno = std::to_string(cardno);
        strmemno = std::to_string(memno);

    gotoxy(1, 15);
    cout << "before update sql";
    cin.get();

        sql = "update tableMemberRecords set amountdeposited = '"+stramt+"' where membershipno = '"+strmemno+"';";

        qstate = mysql_query(mysql, sql.c_str());
        if(!qstate)
        {
            gotoxy(2, 16);
            cout << "Member Record is updated and updated record is : ";
            gotoxy(1, 17);
            mem2->memberSearch(memno);//, 0, 15);
            std::cin.get();
         }
    }

    else
    {
        memno = 0;
    }

    sql = "select max(Billno) from tableBilling";
    qstate = mysql_query(mysql, sql.c_str());
    if(!qstate)
    {
        res = mysql_store_result(mysql);
        if((row = mysql_fetch_row(res)) != nullptr)
        {
            if(row[0] == nullptr)
            {
                billno = 0;
            }
            else
            {
                billno = atoi(row[0]);
            }
            strbillno = std::to_string(++billno);
        }
    }

    product *prd = new product;

LABEL1:
        clrscr();
        drawrect();

        sql = "select p.productname, p.rate, quantity, total from tableProductRecords as p, tableBilling as b where billno = '"+
                strbillno+"' and p.productname in (select productname from tableProductRecords where productid =  b.productid);";

    qstate = mysql_query(mysql, sql.c_str());

    if(!qstate)
    {
        gotoxy(1, 13);
        res = mysql_store_result(mysql);

        sqlprocess *sqlp = new sqlprocess;

        numrowsaffected = sqlp->process_result_set(mysql, res);
    }
    productid = prd->displayProduct("PRODUCT DISPLAY");
    if(productid == 0)
    {
        draw->gotoxy(10, 10);
        cout << "Incorrect Product id";
        cin.get();
//        return;
        goto LABEL1;
    }

    if(productid > 0)
    {
        clrscr();
        drawrect();
        getcmenu->printMenu(salesmenu, "Billing");
        gotoxy(45, 3);
        cout << "Bill No. : " << billno;

        gotoxy(31, 5);
        cout << productid;

        string strpid = std::to_string(productid);
        sql = "select productname, stock, rate from tableProductRecords where productid = '"+strpid+"';";
        qstate = mysql_query(mysql,sql.c_str());
        if(!qstate)
        {
            res = mysql_store_result(mysql);

            if((row = mysql_fetch_row(res)) != nullptr)
            {
                gotoxy(31, 6);
                productname = row[0];
                cout << productname;

                gotoxy(31, 7);
                stock = atoi(row[1]);
                cout << stock;

                gotoxy(31, 8);
                rate = atoi(row[2]);
                cout << rate;
            }
            mysql_free_result(res);
        }

        sql = "select p.productname, p.rate, quantity, total from tableProductRecords as p, tableBilling as b where billno = '"+
                strbillno+"' and productname in (select productname from tableProductRecords where productid =  b.productid);";
        qstate = mysql_query(mysql, sql.c_str());

        if(!qstate)
        {
            gotoxy(1,13);
            res = mysql_store_result(mysql);
            sqlprocess *sqlp = new sqlprocess;
            lines = sqlp->process_result_set(mysql, res);
        }
        do
        {
            gotoxy(31, 9);
            winsize w;
            ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
            for(int x = 31; x <= w.ws_col-1; x++)
            {
                cout << " ";
            }
            gotoxy(31, 9);
            getline(cin, strquantity);
            quantity = Conn::intvalidity(strquantity);

            if(quantity == 0)
            {
                gotoxy(10, 10);
                cout << "Invalid Quantity";
                cin.get();
                gotoxy(31, 9);
            }
        }while(quantity == 0);

        gotoxy(15, 10);
        cout << "Total :         ";

        int qty = stock - quantity;
        if(qty >= 0)
        {
            strqty = std::to_string(qty);
            total = quantity * rate;
            gtotal += total;
            gotoxy(31, 10);
            cout << total;
        }

        else
        {
            gotoxy(10, 12);
            cout << "insufficient stock, available only " << stock;
            cin.get();
            goto LABEL1;
        }

        strstock = std::to_string(stock);
        strquantity = std::to_string(quantity);
        strrate  = std::to_string(rate);
        strtotal = std::to_string(total);

        date *dt = new date;
        time_t now = time(0);
        tm *ltm = std::localtime(&now);
        dt->yy = ltm->tm_year;
        dt->mm = ltm->tm_mon;
        dt->dd = ltm->tm_mday;

        string strdate = std::to_string(1900 + dt->yy) + "/" + std::to_string(dt->mm) + "/" + std::to_string(dt->dd);
        sql = "select max(id) from tableBilling;";
        qstate = mysql_query(mysql, sql.c_str());
        int maxid;
        if(!qstate)
        {
            res = mysql_store_result(mysql);
            if((row = mysql_fetch_row(res)) != nullptr)
            {
                if(row[0] == nullptr)
                {
                    maxid = 0;
                }
                else
                {
                    maxid = atoi(row[0]);
                }
                ++maxid;
                strmaxid = std::to_string(maxid);
            }
            mysql_free_result(res);
        }
        strmemno = std::to_string(memno);

        sql = "insert into tableBilling values ('"+strmaxid+"','"+strpid+"','"+strquantity+"','"+
                                                strtotal+"', '"+strbillno+"','"+strmemno+"','"+strdate+"');";

        qstate = mysql_query(mysql, sql.c_str());
        if(!qstate)
        {
            gotoxy(11, 11);
            cout << "Billing data inserted ";
            sql= "update tableProductRecords as p set p.stock = "+strqty+" where p.productid = "+strpid+";";
            qstate = mysql_query(mysql, sql.c_str());
            if(!qstate)
            {
                gotoxy(10, 12);
                cout << "stock data updated successfully";
                gotoxy(31, 7);
                cout << strqty;

                cin.get();

            }
            else
            {
                gotoxy(10, 12);
                cout << "error in billing data inserted " << mysql_error(mysql);
                cin.get();
            }

        }
        else
        {
            gotoxy(10, 11);
            cout << "Error in insert : " << mysql_error(mysql);
            cin.get();
            return;
        }

        sql = "select p.productname, p.rate, quantity, total from tableProductRecords as p, tableBilling as b where billno = '"+
                strbillno+"' and productname in (select productname from tableProductRecords where productid =  b.productid);";

        qstate = mysql_query(mysql, sql.c_str());

        if(!qstate)
        {
            gotoxy(1, 13);
            res = mysql_store_result(mysql);
            sqlprocess *sqlp = new sqlprocess;

            lines = sqlp->process_result_set(mysql, res);
        }
        else
        {
            gotoxy(1, 13);
            cout << "error in select billing " << mysql_error(mysql);
        }

        gotoxy(10, 12);
        winsize w;
        ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
        for(int i = 10; i < w.ws_col -1; i++)
        {
            cout << " ";
        }

        gotoxy(31, 17 + lines);
        cout << "Grand Total : " << gtotal;

        gotoxy(11,12);
        cout << "want to add more records : ";
        cin >> yesno;
        cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');



        if(toupper(yesno) == 'Y')
        {

            goto LABEL1;
        }

        if(less != 1)
        {
            float discount = 0.05 * gtotal;
            gotoxy(31, 18 + lines);
            cout << "Discount Rs. : " << discount;
            gotoxy(31, 19 + lines);
            cout << "Net Total : " << less * gtotal;
        }
    }

    mysql_free_result(res);
    mysql_close(mysql);

//    delete con;
    delete draw;
    delete mysql;
    delete res;
}
i don't know why this error is occuring. Please sugest me.
We have suggested SEVERAL TIMES in the past that you:
  • Comment your code so we know what it's doing
  • Read the LQ Rules and "Question Guidelines"
  • As a CLEAR QUESTION, which means showing the actual error(s), giving us samples of input data, etc. Just posting a lump of code and asking us to debug your project is fairly rude.
See the numerous other threads where you've done (and continue) this behavior:
https://www.linuxquestions.org/quest...es-4175686406/
https://www.linuxquestions.org/quest...-a-4175683350/
https://www.linuxquestions.org/quest...27-4175673488/

Again, you've been working in C programming for over ten years; is there some reason you can't debug your code?
 
1 members found this post helpful.
Old 12-10-2020, 11:37 AM   #5
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
What have you done to debug your code? Saying that you see a segment violation is a fundamental problem which can absolutely be debugged, either running from within a debugger, or getting a core dump and using the debugger to post analyze what occurred. There's a blog in my signature which covers this, or you can determine how to debug from some web searching.

Not for nothing but this is 463 lines of code you pasted here and there's no main() function, so you've posted just a portion of things. Not that I'm asking to see it all, sorry but I personally am not inclined to read it all but instead to query whether or not you've debugged it using any methods, either with a debugger, or even simple print statements.
 
1 members found this post helpful.
Old 12-10-2020, 12:05 PM   #6
SoftSprocket
Member
 
Registered: Nov 2014
Posts: 399

Rep: Reputation: Disabled
One thing that can help you with debugging (and growing as a programmer) is to improve your organizational skills. Break your function in to smaller units, convert unnecessary globals into local variables and implement proper error handling.
 
1 members found this post helpful.
  


Reply

Tags
c++11, centos7, mysql



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
mysql_query($sql) or die(mysql_error()); in a function, is it possible? konqi Programming 11 04-30-2008 03:27 AM
PHP + mysql_query error ckoniecny Programming 1 03-16-2006 07:32 PM
PHP mysql_query not processing nro Programming 1 09-19-2004 09:43 PM
mysql_query() Error ghight Programming 2 07-29-2004 02:20 PM
PHP - mysql_query won't work - no feedback! jimieee Programming 1 05-19-2004 05:29 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration