【发布时间】:2012-05-25 06:54:17
【问题描述】:
int client::add_user(item & item_in)
{
char temp[ASIZE];
cout << "\n\nEnter the name of your item: ";
cin.get(temp, 100, '\n');
cin.ignore(100, '\n');
get_upper(temp);
item_in.name = temp;
cout << "\n\nEnter in effect one: ";
cin.get(temp, 100, '\n');
cin.ignore(100, '\n');
get_upper(temp);
item_in.effect1 = temp;
cout << "\n\nEnter in effect two: ";
cin.get(temp, 100, '\n');
cin.ignore(100, '\n');
get_upper(temp);
item_in.effect2 = temp;
cout << "\n\nEnter in effect three: ";
cin.get(temp, 100, '\n');
cin.ignore(100, '\n');
get_upper(temp);
item_in.effect3 = temp;
cout << "\n\nEnter in effect four: ";
cin.get(temp, 100, '\n');
cout << "this";
cin.ignore(100, '\n');
cout << "that";
get_upper(temp);
item_in.effect4 = temp;
... 了解我确信这段代码有很多问题,我遇到的问题是前四个块运行得很好,但是当我使用 g++ 编译这段代码并运行它时,显示“this”,然后是分段错误没有“那个”。有什么想法吗?
【问题讨论】:
-
ASIZE的定义是什么? -
@JoachimPileborg const int ASIZE = 30;
-
您是否确定它在那里发生了段错误,而不是在系统开始刷新 cout 之前发生在其他地方(在此代码之后)的段错误? (尝试在“那个”之后添加 cout.flush() )。更好的是,使用调试器逐步完成它,它会准确地告诉你它失败的地方。
-
关于@brepro 的评论,您也可以在调试器中运行程序来查找崩溃的位置。
-
item_in.name = temp这样的几行是可疑的。item_in.name是什么?看起来您正在将临时数组的地址分配给变量。当函数返回并且数组超出范围时,这是一个问题。
标签: c++ arrays segmentation-fault cin