我在使用xcode读取文件时遇到问题所以我决定切换到终端进行测试,但是在终端中,当执行以下函数时(仅当选择1时),我遇到了分段错误。有什么想法?
原来我在阅读文件时遇到了问题。我将发布另一个处理该问题的问题。
struct system {
character player;
};
struct character {
string name;
ushort money;
ushort intelligence;
ushort time;
ushort steps;
ushort score;
};
bool get_number ( int& number )
{
while ( !( cin >> number ) ) {
if ( cin.eof() )
return false;
else {
char ch;
cin.clear();
cout<<"Invalid input, please try again: ";
while (cin.get(ch) && ch != '\n' );
}
}
return true;
}
void viewMenu(menu menuChoice) {
cout << menuChoice.options;
cout << "\nPlease choose an option: ";
int number;
get_number(number);
if (menuChoice.choice == 0) {
if (number == 1) {
struct system game;
startGame(playerName);
}
else if (number == 2) {
viewScores(gameScoreboard);
viewMenu(startMenu);
}
else {
cout << "Goodbye";
exit(0);
}
}
同样,这一切在xcode中工作得很好,但在终端中,当选择“1”时它会失败。