返回值时布尔检查表现异常
#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!";
}
}
这是我的代码,我试图将一个字符串传递到一个布尔函数中,如果传递的字符串满足所有条件,该函数将返回 true。 “通过”单词的资格是它只包含字母 af (无论哪种情况),因此像 AaAa 或 Cafe 或 Bad 这样的单词应该通过,但经过反复试验,即使是我知道应该通过的单词也失败了,我觉得就像我通过递增变量 (isTrueCounter) 来正确跟踪字母的限定条件一样,以计算字符串中的所有字符是否都是限定字符,但即使函数应该返回 true,也会显示 false 情况。我做错了什么?我没看到什么?当我运行此代码时,它将显示变量以帮助跟踪何时将内容添加到持有者变量中,但即使所有数字都正确,也会显示错误的情况。
#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!";
}
}
Here is my code, I am attempting to pass a string into a boolean function that will return true if all criteria is met by the string passed. The qualifications for a "passing" word is that it contains ONLY the letters a-f (of either case) so words like AaAa or Cafe or Bad should pass, but after trial and error, even words that I know should pass are failing and I feel like I am keeping track of the letters' qualifications properly, by incrementing on a variable (isTrueCounter) to count if all the characters in the string are qualifying characters, but even when the function should be returning true, the false case displays. What am I doing wrong? What am I not seeing? When I run this code it will display the variables to help keep track of when stuff is being added to the holder variables but even when all the numbers are right the false case displays.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有返回任何东西,您可以通过多种方式做到这一点。例如,在您的代码中,在条件不成立后立即
返回 false
(不在 af 中)。但您的某些代码可以改进。
1.我不知道你为什么要添加这些行。除非
word.length()
为 0,否则这永远不会成立。2.不要将可以在一个
if
中写入的条件拆分为多个if
,这是一种不好的行为。这更好。
3.您可以比较
char
,而不是检查word[i] == 'a'...
使用>; < >= <=
。4.
string
已经在iostream
中,您不必再次导入它。总之你的代码可以变成这样
You didn't return anything, you can do this in many way. For example from your code,
return false
immediatly after condition is not true (not in a-f).But some of your code can improve.
1.I don't know why you add these lines. This will never true unless
word.length()
is 0.2.Don't split condition that can write in one
if
into manyif
, it's a bad behavior.This is better.
3.You can compare
char
, instead of check ifword[i] == 'a'...
Use> < >= <=
.4.
string
is already iniostream
you don't have to import it again.In conclusion your code can turn into this