#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 case。我究竟做錯了什么?我沒看到什么?當我運行此代碼時,它將顯示變數以幫助跟蹤何時將內容添加到持有者變數中,但即使所有數字都正確,也會顯示錯誤的情況。
uj5u.com熱心網友回復:
你沒有回傳任何東西,你可以通過多種方式做到這一點。例如,從您的代碼中,return false緊接在條件不為真之后(不在 af 中)。
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.我不知道你為什么添加這些行。word.length()除非為 0 ,否則這永遠不會成立。
if (isTrueCounter == word.length())
{
return true;
}
2.不要將可以寫入的條件拆分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.您可以比較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;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/444269.html
上一篇:CPP列舉作為模板標志
下一篇:C ,輸出中沒有任何內容
