布尔检查在返回值时出现意外行为
boolean check behaving unexpectedly when returning value
#include <iostream>
#include <string>
bool is_favorite(std::string word)
{
int isTrueCounter = 0;
std::cout << "\nisTrueCounter: " << isTrueCounter;
if (isTrueCounter == word.length())
{
return true;
}
else {
for (int i = 0; i < word.length(); i++)
{
if (word[i] == 'a' || word[i] == 'A')
{
isTrueCounter++;
std::cout << "\nisTrue +1 ";
}
else
{
if (word[i] == 'b' || word[i] == 'B')
{
isTrueCounter++;
std::cout << "\nisTrue +1 ";
}
else
{
if (word[i] == 'c' || word[i] == 'C')
{
isTrueCounter++;
std::cout << "\nisTrue +1 ";
}
else {
if (word[i] == 'd' || word[i] == 'D')
{
isTrueCounter++;
std::cout << "\nisTrue +1 ";
}
else
{
if (word[i] == 'e' || word[i] == 'E')
{
isTrueCounter++;
std::cout << "\nisTrue +1 ";
}
else
{
if (word[i] == 'f' || word[i] == 'F')
{
isTrueCounter++;
std::cout << "\nisTrue +1 ";
}
}
}
}
}
}
} //for
}
std::cout << "\nisTrueCounter: " << isTrueCounter;
}
int main()
{
std::string favWord;
std::cout << "Please input your word: ";
std::cin >> favWord;
std::cout << "\nFavWord Length: " << favWord.length();
if (is_favorite(favWord) == true)
{
std::cout << "\nThis is a favorite word!";
}
else
{
std::cout << "\nThis is NOT a favorite word!";
}
}
这是我的代码,我试图将一个字符串传递给一个布尔函数,如果传递的字符串满足所有条件,该函数将 return 为真。 “通过”词的条件是它只包含字母 a-f(无论哪种情况),所以像 AaAa 或 Cafe 或 Bad 这样的词应该通过,但经过反复试验,即使是我知道应该通过的词也失败了,我觉得就像我通过递增变量 (isTrueCounter) 来正确跟踪字母的限定条件来计算字符串中的所有字符是否都是限定字符,但即使函数应该 returning 为真,假案例展示。我究竟做错了什么?我没看到什么?当我 运行 这段代码时,它将显示变量以帮助跟踪何时将内容添加到 holder 变量,但即使所有数字都正确,也会显示错误的情况。
你没有return任何东西,你可以通过多种方式做到这一点。例如,根据您的代码,return false
紧随条件不成立(不在 a-f 中)。
bool is_favorite(std::string word)
{
int isTrueCounter = 0;
std::cout << "\nisTrueCounter: " << isTrueCounter;
if (isTrueCounter == word.length())
{
return true;
}
else {
for (int i = 0; i < word.length(); i++)
{
if (word[i] == 'a' || word[i] == 'A')
{
isTrueCounter++;
std::cout << "\nisTrue +1 ";
}
else
{
if (word[i] == 'b' || word[i] == 'B')
{
isTrueCounter++;
std::cout << "\nisTrue +1 ";
}
else
{
if (word[i] == 'c' || word[i] == 'C')
{
isTrueCounter++;
std::cout << "\nisTrue +1 ";
}
else {
if (word[i] == 'd' || word[i] == 'D')
{
isTrueCounter++;
std::cout << "\nisTrue +1 ";
}
else
{
if (word[i] == 'e' || word[i] == 'E')
{
isTrueCounter++;
std::cout << "\nisTrue +1 ";
}
else
{
if (word[i] == 'f' || word[i] == 'F')
{
isTrueCounter++;
std::cout << "\nisTrue +1 ";
} else { //add this else clause
return false;
}
}
}
}
}
}
} //for
}
std::cout << "\nisTrueCounter: " << isTrueCounter;
}
但是您的一些代码可以改进。
1.I不知道你为什么要加上这些行。这永远不会成立,除非 word.length()
为 0。
if (isTrueCounter == word.length())
{
return true;
}
2.Don没有把一个if
写成多个if
的条件拆分成很多if
,这是不好的行为。
if (word[i] == 'a' || word[i] == 'A')
{
isTrueCounter++;
std::cout << "\nisTrue +1 ";
}
else
{
if (word[i] == 'b' || word[i] == 'B')
{
isTrueCounter++;
std::cout << "\nisTrue +1 ";
}
这样更好。
if (word[i] == 'a' || word[i] == 'A' || word[i] == 'b' || word[i] == 'B')
{
isTrueCounter++;
std::cout << "\nisTrue +1 ";
}
3.You 可以比较 char
,而不是检查 word[i] == 'a'...
使用 > < >= <=
.
if (word[i] >= 'a' && word[i] <= 'f' || word[i] >= 'A' && word[i] <= 'F')
{
isTrueCounter++;
std::cout << "\nisTrue +1 ";
}
4.string
已经在 iostream
中,您不必再次导入它。
总之你的代码可以变成这样
bool is_favorite(std::string word)
{
int isTrueCounter = 0;
for (int i = 0; i < word.length(); i++)
{
if (word[i] >= 'a' && word[i] <= 'f' || word[i] >= 'A' && word[i] <= 'F') {
isTrueCounter++;
std::cout << "\nisTrue +1 ";
} else {
return false;
}
}
std::cout << "\nisTrueCounter: " << isTrueCounter;
return true;
}
#include <iostream>
#include <string>
bool is_favorite(std::string word)
{
int isTrueCounter = 0;
std::cout << "\nisTrueCounter: " << isTrueCounter;
if (isTrueCounter == word.length())
{
return true;
}
else {
for (int i = 0; i < word.length(); i++)
{
if (word[i] == 'a' || word[i] == 'A')
{
isTrueCounter++;
std::cout << "\nisTrue +1 ";
}
else
{
if (word[i] == 'b' || word[i] == 'B')
{
isTrueCounter++;
std::cout << "\nisTrue +1 ";
}
else
{
if (word[i] == 'c' || word[i] == 'C')
{
isTrueCounter++;
std::cout << "\nisTrue +1 ";
}
else {
if (word[i] == 'd' || word[i] == 'D')
{
isTrueCounter++;
std::cout << "\nisTrue +1 ";
}
else
{
if (word[i] == 'e' || word[i] == 'E')
{
isTrueCounter++;
std::cout << "\nisTrue +1 ";
}
else
{
if (word[i] == 'f' || word[i] == 'F')
{
isTrueCounter++;
std::cout << "\nisTrue +1 ";
}
}
}
}
}
}
} //for
}
std::cout << "\nisTrueCounter: " << isTrueCounter;
}
int main()
{
std::string favWord;
std::cout << "Please input your word: ";
std::cin >> favWord;
std::cout << "\nFavWord Length: " << favWord.length();
if (is_favorite(favWord) == true)
{
std::cout << "\nThis is a favorite word!";
}
else
{
std::cout << "\nThis is NOT a favorite word!";
}
}
这是我的代码,我试图将一个字符串传递给一个布尔函数,如果传递的字符串满足所有条件,该函数将 return 为真。 “通过”词的条件是它只包含字母 a-f(无论哪种情况),所以像 AaAa 或 Cafe 或 Bad 这样的词应该通过,但经过反复试验,即使是我知道应该通过的词也失败了,我觉得就像我通过递增变量 (isTrueCounter) 来正确跟踪字母的限定条件来计算字符串中的所有字符是否都是限定字符,但即使函数应该 returning 为真,假案例展示。我究竟做错了什么?我没看到什么?当我 运行 这段代码时,它将显示变量以帮助跟踪何时将内容添加到 holder 变量,但即使所有数字都正确,也会显示错误的情况。
你没有return任何东西,你可以通过多种方式做到这一点。例如,根据您的代码,return false
紧随条件不成立(不在 a-f 中)。
bool is_favorite(std::string word)
{
int isTrueCounter = 0;
std::cout << "\nisTrueCounter: " << isTrueCounter;
if (isTrueCounter == word.length())
{
return true;
}
else {
for (int i = 0; i < word.length(); i++)
{
if (word[i] == 'a' || word[i] == 'A')
{
isTrueCounter++;
std::cout << "\nisTrue +1 ";
}
else
{
if (word[i] == 'b' || word[i] == 'B')
{
isTrueCounter++;
std::cout << "\nisTrue +1 ";
}
else
{
if (word[i] == 'c' || word[i] == 'C')
{
isTrueCounter++;
std::cout << "\nisTrue +1 ";
}
else {
if (word[i] == 'd' || word[i] == 'D')
{
isTrueCounter++;
std::cout << "\nisTrue +1 ";
}
else
{
if (word[i] == 'e' || word[i] == 'E')
{
isTrueCounter++;
std::cout << "\nisTrue +1 ";
}
else
{
if (word[i] == 'f' || word[i] == 'F')
{
isTrueCounter++;
std::cout << "\nisTrue +1 ";
} else { //add this else clause
return false;
}
}
}
}
}
}
} //for
}
std::cout << "\nisTrueCounter: " << isTrueCounter;
}
但是您的一些代码可以改进。
1.I不知道你为什么要加上这些行。这永远不会成立,除非 word.length()
为 0。
if (isTrueCounter == word.length())
{
return true;
}
2.Don没有把一个if
写成多个if
的条件拆分成很多if
,这是不好的行为。
if (word[i] == 'a' || word[i] == 'A')
{
isTrueCounter++;
std::cout << "\nisTrue +1 ";
}
else
{
if (word[i] == 'b' || word[i] == 'B')
{
isTrueCounter++;
std::cout << "\nisTrue +1 ";
}
这样更好。
if (word[i] == 'a' || word[i] == 'A' || word[i] == 'b' || word[i] == 'B')
{
isTrueCounter++;
std::cout << "\nisTrue +1 ";
}
3.You 可以比较 char
,而不是检查 word[i] == 'a'...
使用 > < >= <=
.
if (word[i] >= 'a' && word[i] <= 'f' || word[i] >= 'A' && word[i] <= 'F')
{
isTrueCounter++;
std::cout << "\nisTrue +1 ";
}
4.string
已经在 iostream
中,您不必再次导入它。
总之你的代码可以变成这样
bool is_favorite(std::string word)
{
int isTrueCounter = 0;
for (int i = 0; i < word.length(); i++)
{
if (word[i] >= 'a' && word[i] <= 'f' || word[i] >= 'A' && word[i] <= 'F') {
isTrueCounter++;
std::cout << "\nisTrue +1 ";
} else {
return false;
}
}
std::cout << "\nisTrueCounter: " << isTrueCounter;
return true;
}