ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.