我有一些需要多次调用的函数,因此
int i;
i = 10;
while (i > 0)
{
selectletter(wordArray);
computerTurn(wordArray);
printGrid(grid);
i--;
}
函数选择工作正常,并且在该函数的末尾附近,它调用另一个函数“claimword”。 Claimword运行完全正常,但在函数结束时,程序在上下文用完时崩溃,而不是像上面所示那样转移到计算机上。我抬头看着如何“退出”一个功能,每个人都说“返回”。即使在虚函数中也能正常工作。但是,当我尝试使用return时,除了return语句被忽略之外的任何内容之外什么也没发生。谁能告诉我为什么return语句不起作用?
void claimword(Tile grid[7][6], char letter, string wordArray[100])
{
cout << "Would you like to claim a word? (Y/N)" << endl;
char chooseinput;
cin >> chooseinput;
if ((chooseinput == 'y') || (chooseinput == 'Y'))
{
printGrid(grid);
cout << "Please enter the word you would like to claim." << endl;
string input;
cin >> input;
int inthegrid = 0;
int errormessage = 0;
compchecker(grid, input, inthegrid);
int length;
if (inthegrid = 1)
{
for(int i = 0; i < 100; ++i)
{
if (input == wordArray[i])
{
if (input.find(letter) != std::string::npos)
{
string strl;
strl = wordArray[i];
length = strl.length();
cout << "You have claimed the word " << strl << endl;
wordArray[i] = "/";
}
else
{
errormessage = 1;
}
}
else
{
///cout << "Sorry, that word is not in the dictionary." << endl;
errormessage = 2;
}
}
if (errormessage = 1)
{
cout << "Sorry you cannot claim that word." << endl;
}
if (errormessage = 2)
{
cout << "Sorry, that word is not in the dictionary." << endl;
}
if (length == 3)
{
human.humanpoints = human.humanpoints + 1;
wordsthisturn = wordsthisturn + 1;
cout << "You have earned one point!" << endl;
}
if (length == 4)
{
human.humanpoints = human.humanpoints + 2;
wordsthisturn = wordsthisturn + 2;
cout << "You have earned two points!" << endl;
}
if (length == 5)
{
human.humanpoints = human.humanpoints + 4;
wordsthisturn = wordsthisturn + 4;
cout << "You have earned four points!" << endl;
}
if (length == 6)
{
human.humanpoints = human.humanpoints + 8;
wordsthisturn = wordsthisturn + 8;
cout << "You have earned eight points!" << endl;
}
if (length == 7)
{
human.humanpoints = human.humanpoints + 16;
wordsthisturn = wordsthisturn + 16;
cout << "You have earned sixteen points!" << endl;
}
else
{
cout << "Your word was too small to claim any points." << endl;
}
}
}
else
{
cout << "End of Player Turn." << endl;
//return;
}
cout <<"Test1";
return;
cout <<"Test2";
}
无论我给它的输入(y / n等),“Test1”显示,但“Test2”不显示。我的理论是程序不会一直返回,或者我只是没有正确使用它。
编辑:
在主函数中使用已编辑的语句
selectletter(wordArray);
cout << "test11";
computerTurn(wordArray);
应该发生的是应该调用selectletter函数。在其末尾的selectletter函数调用另一个函数,claimWord。 claimWord上面发布。在函数结束时,它应该结束。应该没有什么可以做的,并且在所有关于点的if / elses之后,即使没有得分,或者函数中的任何事情发生,该函数应该结束。然后程序应显示“test11”,但不会显示。
EDIT2:
void selectletter(string wordArray[100])
{
cout << endl;
cout << "REMAINING LETTERS:" << endl;
cout << human.humanletters << endl;
cout << "Select a letter.";
int length;
length = human.humanletters.size();
char input;
cin >> input;
int column;
int row = 7;
int cinput;
//mght have to change since 0 is the first val
cout << "What column would you like to drop that in? (1-7)";
cin >> cinput;
column = cinput - 1;
//cout << "Test1";
while (row > 0)
{
if (grid[row-1][column].active == true)
{
row--;
//cout << "Test3";
}
else
for(int i = 0; i < length; i++)
{
if(human.humanletters[i] == input)
{
//cout << "Test5";
human.humanletters.erase(std::remove(human.humanletters.begin(), human.humanletters.end(), input), human.humanletters.end());
grid[row-1][column].letter = input;
grid[row-1][column].active = true;
cout << endl;
//cout << "Test6";
claimword(grid, input, wordArray);
//this removes ALL instances of the letter, however
}
break;
//need to add something for if the letter is not in the string
//}
//row = 9999;
}
}
}
答案 0 :(得分:1)
无论我给它的输入(y / n等),“Test1”显示,但“Test2”不显示。
这就是应该做的事情。您在显示return
之后和显示Test1
之前致电Test2
,因此跳过后者。 return
是立即返回调用当前函数的函数。
答案 1 :(得分:0)
您的while循环的条件为while (row > 0)
,而row
时只会递减(grid[row-1][column].active == true)
。如果评估为false
,则不会减少row
并且您的程序将永远运行。
也许你的break;
意味着打破了while循环,但它所做的只是打破for循环。 break
语句突破最近的封闭循环/切换块。