Hi I'm working on a program that compares the users response to an answer like a quiz. At first I wasn't sure how to do this using cin because the answer was two words. Someone helped me out and showed me the getline command. The only problem is if i enter the answers in all lowercase the user has too as well. Any suggestions.

Heres an example of my code

string answer26 = "sulfur trioxide";
    string userresponse26;
    cout << "\n26.  SO3:\n\t";
    getline(cin,userresponse26);
    if (userresponse26 == answer26)
    {
                 cout << "Correct" << endl;
                 score += 1;
    }
    else
    {
                 cout << "Incorrect" << endl;
    }

Hi I'm working on a program that compares the users response to an answer like a quiz. At first I wasn't sure how to do this using cin because the answer was two words. Someone helped me out and showed me the getline command. The only problem is if i enter the answers in all lowercase the user has too as well. Any suggestions.

Heres an example of my code

string answer26 = "sulfur trioxide";
    string userresponse26;
    cout << "\n26.  SO3:\n\t";
    getline(cin,userresponse26);
    if (userresponse26 == answer26)
    {
                 cout << "Correct" << endl;
                 score += 1;
    }
    else
    {
                 cout << "Incorrect" << endl;
    }

http://www.cplusplus.com/reference/clibrary/cctype/toupper/

In Java, there is a function called equalsIgnoreCase . To my knowledge, no such function exists in the C++ standard libraries, so you'll have to write your own function. The link above may help you do that. It operates on single characters and converts them to upper case. Thus toupper ('d') yields 'D'. I would write an equalsIgnoreCase (string, string) function in C++ and call it. Go through the strings character by character and compare the toupper values of the characters. If the two strings are the same length and the topper values are the same, equalsIgnoreCase should return true. Otherwise, it returns false.

string string1 = "dog";
string string2 = "doG";

if (equalsIgnoreCase (string1, string2))
    cout << "They are the same.";

there is a function in the <string> header file called _stricmp that will convert all the characters to there lowercase. the function is defined as

int _stricmp( const char *string1, const char *string2 );

this function will return 0 if the strings are equal. less than 0 and greater than zero returns mean they do not match. to use this i would write

#include <string>
#include <iostream>

using namespace std;

int main()
{
string string1 = "abcd";
string string2 = "ABCD";
int equal;
equal = _stricmp(string1.c_str(), string2.c_str());
// using the c_str function here to pass the char array to the
 //function because it does not take in a string but a char
if (equal == 0)
cout << "the strings are equal"
else
cout << "the strings are not equal"
return 0;
}

hope this helps you out

commented: Bad advice in a number of ways. -4

there is a function in the <string> header file called _stricmp that will convert all the characters to there lowercase. the function is defined as

int _stricmp( const char *string1, const char *string2 );

this function will return 0 if the strings are equal. less than 0 and greater than zero returns mean they do not match. to use this i would write

Very interesting. I wasn't aware of this function. It worked in Dev C++. I looked in the string and cstring libraries on cplusplus.com and didn't see the specification you refer to. However, it did compile and work, so it must be somewhere. Is _stricmp a standard function? I've been browsing and saw some threads that say that it is a Windows-specific function.

it might be windows specific but I'm not sure. i am using Microsoft visual c++ 6.0 professional and found it in my msdn one day when trying to find an answer to this specific problem for use with a very basic login screen and it didn't care about case sensitivity in the passwords and user names. all of the code that i have made so far has been on windows machines so i couldn't tell you if it is portable to other OS's. i have include my string.h file so that you can see the function prototype. the function is marked with a comment for you.

Thank you very much for the help. Worked perfectly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.