HELP PREASSSEEEEE

1
2
3
4
5
6
7
8
9
10
bool simpleQuestion::checkAnswer(string guess) const
{
    for (int i=0;i<qAnswer.length();i++)
        
        
    if (qAnswer==guess)
        return true;
    else
        return false;
}



this is my code and what im trying to accomplish here is making the comparison of the two strings (qAnswer & guess) case insensitive. I know i need to loop through each character of each string and for each character i can use toupper and every char in each string will become uppercase and therefore case insensitive. However, im not sure how to go about this; if anyone could show me a technique used to loop through each character of the string and how to use toupper that would be great!
also i just noticed something... this function is constant so within the function i cant actually alter the calling object.
is this correct?
1
2
3
4
5
6
7
8
9
bool simpleQuestion::checkAnswer( std::string guess ) const
{
    for( char& c : guess ) c = std::toupper(c) ; // convert guess to upper case

    std::string temp = qAnswer ; // make a copy of qAnswer
    for( char& c : temp ) c = std::toupper(c) ; // convert it to upper case

    return guess == temp ; // compare for equality
}



> this function is constant so within the function i cant actually alter the calling object.
> is this correct?

Yes.
Last edited on
Topic archived. No new replies allowed.