時,我有這樣的:段錯誤獲取用戶輸入
// Get database access parameters
const char* db = "codes", *server = "localhost", *user = "root", *pass = "pass";
// Connect to the sample database.
mysqlpp::Connection conn(false);
if (conn.connect(db, server, user, pass)) {
for (int i=0; i<10; ++i)
{
int d, count;
cout << "Введите D\n";
cin >> d;
cout << "Введите количество записей при D=" << d << endl;
cin >> count;
for (int a=0; a<count; ++a)
{
char * name;
int r,n1,n2;
cout << "Введите R\n";
cin >> r;
cout <<"Введите n1 и n2\n";
cin >> n1 >> n2;
cout <<"Введите название\n";
cin >> name;
mysqlpp::Query query = conn.query();
for (int j=n2-n1+1; j<n2; ++j)
{
int k =pow(2,(j+r));
query << "insert into code (n,k,d, name) values (" << j << "," << k << "," <<d<<"," << mysqlpp::quote_only << name << ");";
query.execute();
}
}
}
conn.disconnect();
return 0;
}
else {
cerr << "DB connection failed: " << conn.error() << endl;
return 1;
}
}
對不起張貼的所有代碼。 我可以編譯程序沒有錯誤,但在命令行中,它說「分段錯誤」。這是什麼錯誤?以及如何解決它?
你應該讓你的下一個任務學習使用調試器,例如gdb。一個調試器會告訴你,你正在'cin >> name'行中崩潰,而'name'不是一個合理的指針。 – derobert
如果你是在Linux上,嘗試[Valgrind的(http://stackoverflow.com/questions/7316306/c-segmentation-fault-with-strcmp/7316492#7316492) –