如果语句导致循环没有完全运行? C ++

时间:2017-10-22 07:22:16

标签: c++

for (int k = 0; k != s.size(); k++)
{
    if (s[k] == 'A' || s[k] == 'B' || s[k] == 'C' || s[k] == 'D' )
        return true; 
    else 
        return false;
}

我正在尝试检查输入的字符串是否包含除这些之外的任何字符。如果是的话,我希望它返回false。但是,当我输入像“ABCEDF”这样的字符串时,它返回true。看起来它只是检查第一个字符/索引。如何使用for循环和if语句来检查输入的字符串是否符合正确的条件并相应地返回true / false?

3 个答案:

答案 0 :(得分:4)

问题是你真的在检查第一个字符是否是if中列出的字符之一。 该错误是算法的,因为为了确定该字符串是否包含除ABCD以外的字符,您必须至少读取输入字符串的每个单个字符。 这意味着: 如果您读取与ABCD不同的内容,则在循环内返回true。 如果你已经阅读了整个字符串但仍然没有返回,那么,猜猜看,字符串只由ABCD组成,所以你可以返回false!

另外,如果你想检查字符串是否包含ABCD以外的任何字符,你应该检查s[k]!=A && s[k]!=B && s[k]!=C && s[k]!=D

如下所示,应该有效:

bool otherThanABCD(const string& s){
    for (int k = 0; k != s.size(); k++){
        if (!(s[k] == 'A' || s[k] == 'B' || s[k] == 'C' || s[k] == 'D') )    
    // there is a char which is not ABC or D!
          return true; 
   }
    // I have read the whole string, but still nothing other than ABC and D
 return false;
}

答案 1 :(得分:3)

如果条件为true,则返回true。如果条件为false,则返回false。在 not return的情况下没有分支。

您的代码等同于

for (int k = 0; k != s.size(); k++)
{
    return (s[k] == 'A' || s[k] == 'B' || s[k] == 'C' || s[k] == 'D' );
}

换句话说,你无条件地返回。

如果不确切知道自己想做什么或做什么,我 你可能会想要这样的事情:

for (int k = 0; k != s.size(); k++)
{
    if (s[k] == 'A' || s[k] == 'B' || s[k] == 'C' || s[k] == 'D' )
        return true;
}

return false;

在循环之后只返回的地方。

答案 2 :(得分:0)

你真的不需要循环和一堆if语句来完成这个任务,你可以编写一个简单的函数来为你做这个。检查这段代码!

#include <string>
#include <iostream>

// This checkString function is all you really need!
bool checkString( const std::string& stringToCheck, const std::string& checkAgainstThis ) {
    if ( stringToCheck.empty() || checkAgainstThis.empty() ) {
        // throw exception - both strings have nothing in them!
    }

    std::basic_string<char>::size_type firstIndex = stringToCheck.find_first_not_of( checkAgainstThis );
    if ( firstIndex == std::string::npos ) {
        return true;  // Didn't find anything.
    }

    return false;  // Found something other than.
}

// helper functions to display messages in main function - these are not required for
// the above function; just makes the main function cleaner and easier to read

void displayCheckingMessage( const std::string& a, const std::string& b ) {
    std::cout << "Checking string " << a << "\nto see if it contains any character other than this set: " << b << ".\n";
}

void displayResultMessage( bool condition, const std::string& str ) {
    if ( condition ) {
        std::cout << "Did not find any other characters.\n\n";
    } else {
        std::cout << "Found something other than " << str << ".\n\n";
    }
}

int main() {    
    std::string stringToCheck( "ABCADCDABC" );
    const std::string checkAgainstThis( "ABCD" );

    displayCheckingMessage( stringToCheck, checkAgainstThis );
    displayResultMessage( checkString( stringToCheck, checkAgainstThis ), checkAgainstThis );

    stringToCheck.clear();
    stringToCheck = std::string( "ABCDE" );
    displayCheckingMessage( stringToCheck, checkAgainstThis );
    displayResultMessage( checkString( stringToCheck, checkAgainstThis ), checkAgainstThis );

    stringToCheck.clear();
    stringToCheck = std::string( "AAABBBCCCDDDAAABBDDCCSSRRAASSGGSS" );
    displayCheckingMessage( stringToCheck, checkAgainstThis );
    displayResultMessage( checkString( stringToCheck, checkAgainstThis ), checkAgainstThis );

    stringToCheck.clear();
    stringToCheck = std::string( "AAAAAAAAAAAAAAAAABBBBBBBBBBBBBAAAAAAAAAAAAAAACCCCCCCCCCCCDDDDDDDDDDD" );
    displayCheckingMessage( stringToCheck, checkAgainstThis );
    displayResultMessage( checkString( stringToCheck, checkAgainstThis ), checkAgainstThis );

    stringToCheck.append( "XABCD" );
    displayCheckingMessage( stringToCheck, checkAgainstThis );
    displayResultMessage( checkString( stringToCheck, checkAgainstThis ), checkAgainstThis );

    // try a different set
    std::cout << "\n";
    const std::string checkAgainstThis2( "XYZ" );
    displayCheckingMessage( stringToCheck, checkAgainstThis2 );
    displayResultMessage( checkString( stringToCheck, checkAgainstThis2 ), checkAgainstThis2 );

    stringToCheck.clear();
    stringToCheck = std::string( "XYZXYZXYZXYZ" );
    displayCheckingMessage( stringToCheck, checkAgainstThis2 );
    displayResultMessage( checkString( stringToCheck, checkAgainstThis2 ), checkAgainstThis2 );

    stringToCheck.append( "8?#XYZXYSAZC" );
    std::cout << "\n" << stringToCheck << "\n";
    displayCheckingMessage( stringToCheck, checkAgainstThis2 );
    displayResultMessage( checkString( stringToCheck, checkAgainstThis2 ), checkAgainstThis2 );

    return 0;
}

使用这个简单的函数,你可以检查任何第一个字符串,看它是否包含你要从第二个字符串检查的字符集以外的任何字符。这也可以防止您在if语句条件中使用“硬编码”字符。这也可以为任何字符串与一组确定的字符进行比较提供额外的好处。