您好我尝试构建我的代码但它抛出一个异常,任何人都可以帮我解决它吗?以下是代码和输出。
谢谢
int main()
{
char input;
while (input!= 'g')
{
cout << "Welcome to my Scramble Word Game!" << endl << endl;
cout << "a) Add Words into game database" << endl;
cout << "b) Remove words" << endl;
cout << "c) configure count down time" << endl;
cout << "d) Start game" << endl;
cout << "e) Select Game level" << endl;
cout << "f) View score chart" << endl;
cout << "g) Quit" << endl;
cout << "Please enter an alphabet to select: ";
cin >> input;
cout << endl;
switch(input)
{
case 'a': cout << "Please enter word not long than 16 characters" << endl;
write();
break;
case 'b': cout << "Please enter word to remove" << endl;
cin >> delword;
removeWord(delword);
break;
case 'c': cout << "c) configure count down time" << endl;
break;
case 'd': cout << "Game Started" << endl;
startGame(sl);
break;
case 'e': cout << "PLease select Game level" << endl;
cin >> sl;
break;
case 'f': cout << "f) View score chart" << endl;
break;
case 'g': cout << "g) Quit" << endl;
break;
default: cout << "Invalid selection, please enter a alphabet"
<< " from the entrees above.\n\n";
break;
}
}
return 0;
}
void removeWord(string r)
{
ifstream wordBase("Words Database.txt",ios::out);
ofstream temp ("temp.txt");
string line = "";
while(getline(wordBase,line))
{
if(line != r)
temp << line << endl;
}
temp.close();
wordBase.close();
remove("Words Database.txt");
rename("temp.txt","Words Database.txt");
}
int write(void)
{
ofstream outfile;
string userInputOfWord;
outfile.open("Words Database.txt" ,ios::app);
cin.clear();
cin >> userInputOfWord;
outfile << userInputOfWord << endl;
outfile.close();
cout << "Word entered into database" << endl << endl;
return 0;
}
void startGame(int sl)
{
{
int sl;
string word;
vector<string> levelList;
srand(time(NULL));
string guess;
string words;
vector<string> wordList;
ifstream in ("Words Database.txt");
while(in >> words)
{
wordList.push_back(words);
}
for(vector<string>::iterator it = wordList.begin(); it != wordList.end(); ++it)
{
cout << *it << ' ';
switch(sl)
{
case 1 : if((*it).length() >= 2 && (*it).length() <= 7)
{
levelList.push_back(*it);
}
break;
case 2 : if((*it).length() >= 8 && (*it).length() <= 15)
{
levelList.push_back(*it);
}
break;
case 3 : if((*it).length() > 15)
{
levelList.push_back(*it);
}
break;
}
}
string jumble = levelList.at(rand()% wordList.size());
word = jumble;
int length = jumble.size();
for (int n=0;n<length;n++)
{
int index1 = (rand()%length);
int index2 = (rand()%length);
char temp = jumble[index1];
jumble[index1]=jumble[index2];
jumble[index2]=temp;
}
cout << "Scrambled word is "<< jumble << endl;
cout << "Enter guess: ";
cin >> guess;
while (guess != word)
{
cout <<"Wrong attempt ... try again";
cout << "\n\nYour guess: ";
cin >> guess;
}
if (guess == word)
cout << "\nYou are correct!\n";
else
cout << "\nThanks for playing.\n";
}
}
欢迎来到我的争夺文字游戏!
a)在游戏数据库中添加单词 b)删除单词 c)配置倒计时 d)开始游戏 e)选择游戏关卡 f)查看得分图表 g)退出 请输入字母表进行选择:d
游戏开始 抛出'std :: out_of_range'实例后调用终止 what():vector :: _ M_range_check 苹果橙甜瓜lemondinosaur 跑完了;中止;实时:1s;用户:0ms; system:0ms
答案 0 :(得分:1)
可能存在问题:
switch(sl)
在此行之前,变量s1
似乎没有在主要内容'e'
之外的任何地方初始化。因此,在任何情况下(1,2,3),程序都不能push_back
levelList
的任何值。
此外,在main中初始化的sl
无法正常使用,因为它在startGame
中重新定义:
void startGame(int sl)
{
{
int sl;//should be removed