Thread: How is a program written to ignore case sensitivity when dealing with input?

  1. #1
    Registered User
    Join Date
    Nov 2014
    Posts
    5

    How is a program written to ignore case sensitivity when dealing with input?

    Hello,

    I've written a simple program, which asks the user to respond to a YES or NO question using the character Y/y for YES and the character N/n for NO. The foundation of this program is based around several IF statements implemented to aid in finding the ASCII value of the character entered before invoking the corresponding cout statement that informs the user which character they entered.

    My Question: How should a program be written to deal with ignoring case sensitivity in regards to the users' input?

    Here is my amateurish attempt,

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        /*program inquires a yes or no response from the user,
        in this case, using the characters "Y/y" and "N/n".*/
        cout << "Do you like programming (Y/y for YES, N/n for NO): ";
        char user_input;
        cin >> user_input;
    
        /*The following are IF statements written for the program to determine whether the user answered YES or NO. 
    The program then performs the cout statement that contains the corresponding character to the ASCII value found.*/
        if(user_input == 121 || user_input == 89)
        {
            if(user_input == 121)
            {
                cout << "\nYou answered YES with the lowercase 'y'." << endl;
                return 0;
            }
            else if(user_input == 89)
            {
                cout << "\nYou answered YES with the UPPERCASE 'Y'." << endl;
                return 0;
            }
            return 0;
        }
        else if(user_input == 110 || user_input == 78)
        {
            if(user_input == 110)
            {
                cout << "\nYou answered NO with the lowercase 'n'." << endl;
                return 0;
            }
            else if (user_input == 78)
            {
                cout << "\nYou answered NO with the UPPERCASE 'N'." << endl;
                return 0;
            }
            return 0;
        }
        
        return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    One approach is to change the string to be in a consistent case, e.g., with the help of std::toupper from <cctype>. In this case though, you only have a single character to compare, so it would suffice to check for 'Y' and 'y'. Do not use magic numbers like 121 and 89.

    By the way, you have an overdose of return 0; in your program
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2014
    Posts
    5
    Thanks! I will do some research over that.

    Quote Originally Posted by laserlight View Post
    By the way, you have an overdose of return 0; in your program
    Haha... I had a feeling I overkilled it with the return 0;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Case Sensitivity
    By bernt in forum General Discussions
    Replies: 6
    Last Post: 10-17-2011, 02:12 PM
  2. Case Sensitivity Help!!!
    By Master Ali in forum C Programming
    Replies: 3
    Last Post: 01-21-2011, 04:38 AM
  3. Case Sensitivity... matching words in a string.
    By braden87 in forum C Programming
    Replies: 5
    Last Post: 03-13-2010, 11:04 PM
  4. case sensitivity in GtkTextView
    By MK27 in forum Linux Programming
    Replies: 2
    Last Post: 08-07-2008, 03:16 PM
  5. strcmp without case sensitivity
    By AmazingRando in forum C Programming
    Replies: 8
    Last Post: 03-16-2005, 08:40 AM