只使用输入的单词一次
Using the inputed word only once
我遇到了用户在输入后无法使用相同单词的问题。
下面是我正在处理的 Boggle 程序的片段。
下面的代码无法按照我想要的方式运行,例如:
(1)用户输入单词:fun --> store into temp[0] --> increment count
(2)用户输入单词:fun --> 重复 while 循环输入另一个不同的输入
(3)用户输入单词:bye --> break while loop --> store into temp[1] --> increment count
(4)用户输入单词:bye 或 fun --> 将重复另一个输入
(5)用户输入单词:good --> store into temp[2] --> increment count
(6)用户输入单词:bye --> 重复 while 循环以获得另一个输入,
(7)用户输入单词:fun --> While循环中断,单词变为VALID。
你看.. 问题是它没有循环回 temp[0] 再次找到 fun 这个词来说明它无效。
如有任何帮助,我们将不胜感激。
//seconds is used with time library, but just ignore its declaration
while (seconds < 300){
string word;
string temp[100];
int count = 0;
cout << "Enter:" << endl;
cin >> word;
for (int i = 0; i < count; i++){
while (word == temp[i]){
cout << "Word was already used. Please type another word." << endl;
cin >> word;
}
}
temp[count] = word;
count = count + 1;
if (seconds >= 300)
break;
}
试试这个:
string word;
string temp[100];
int count = 0;
cout << "Enter:" << endl;
bool found;
do
{
cin >> word;
found = false;
for (int i = 0; i < count; i++)
{
if (temp[i] == word)
{
found = true;
cout << "Word was already used. Please type another word." << endl;
break;
}
}
}
while (found);
temp[count++] = word;
这样一来,如果在 temp
中的任何地方找到 word
,它就会请求一个新词,然后每次都重新检查整个 temp
数组。您的原始代码没有这样做,即使在要求新输入时也只检查当前索引 i
。
我遇到了用户在输入后无法使用相同单词的问题。 下面是我正在处理的 Boggle 程序的片段。
下面的代码无法按照我想要的方式运行,例如:
(1)用户输入单词:fun --> store into temp[0] --> increment count
(2)用户输入单词:fun --> 重复 while 循环输入另一个不同的输入
(3)用户输入单词:bye --> break while loop --> store into temp[1] --> increment count
(4)用户输入单词:bye 或 fun --> 将重复另一个输入
(5)用户输入单词:good --> store into temp[2] --> increment count
(6)用户输入单词:bye --> 重复 while 循环以获得另一个输入,
(7)用户输入单词:fun --> While循环中断,单词变为VALID。
你看.. 问题是它没有循环回 temp[0] 再次找到 fun 这个词来说明它无效。
如有任何帮助,我们将不胜感激。
//seconds is used with time library, but just ignore its declaration
while (seconds < 300){
string word;
string temp[100];
int count = 0;
cout << "Enter:" << endl;
cin >> word;
for (int i = 0; i < count; i++){
while (word == temp[i]){
cout << "Word was already used. Please type another word." << endl;
cin >> word;
}
}
temp[count] = word;
count = count + 1;
if (seconds >= 300)
break;
}
试试这个:
string word;
string temp[100];
int count = 0;
cout << "Enter:" << endl;
bool found;
do
{
cin >> word;
found = false;
for (int i = 0; i < count; i++)
{
if (temp[i] == word)
{
found = true;
cout << "Word was already used. Please type another word." << endl;
break;
}
}
}
while (found);
temp[count++] = word;
这样一来,如果在 temp
中的任何地方找到 word
,它就会请求一个新词,然后每次都重新检查整个 temp
数组。您的原始代码没有这样做,即使在要求新输入时也只检查当前索引 i
。