-4

so I'm learning programming and I understand variables, if else statements, cin and cout. So for a starter project I'm just creating a console application that asks the user questions, such as age, location etc. One of them I would like just a simple yes or no answer. I've managed to do this, but what the user inputs must be the same case as the word in the if statement. i.e. if statement contains "Yes" with a capital 'Y'. If the user inputs "yes" without a capital 'Y' then the program fails.

The if statement sees if it is a "Yes", if it is, provides positive feedback. If "No", then it provides negative feedback.

How can I make it no matter whether the answer is "YES", "yes" or even "YeS"?

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
ShadowGlow
  • 1
  • 1
  • 1

3 Answers3

1

u can take the input string, change it all to upper\lower case and then check if it is "YES" or "yes".

for each char in input: tolower(c)

CIsForCookies
  • 12,097
  • 11
  • 59
  • 124
0

An easy way to do this is to first convert the user input to lowercase letters. And then compare it with a lower case yes or no.

#include <iostream>
// This header contains to tolower function to convert letters to lowercase
#include <cctype>
#include <string>

using namespace std;

int main()
{
    string user_input;
    cin >> user_input;

    // Loop over each letter and change it to lowercase
    for (string::iterator i = user_input.begin(); i < user_input.end(); i++){
        *i = tolower(*i);
    }

    if (user_input == "yes") {
        cout << "You said yes" << endl;
    } else {
        cout << "You did not say yes" << endl;
    }
}
Moyamo
  • 358
  • 2
  • 10
0

you can try this:

int main(void) 
{

    string option;
    cin>>option;
    transform(option.begin(), option.end(), option.begin(), ::tolower);
    if(option.compare("yes")==0){
      cout<<option;
    }
     return 0;
}
Rustam
  • 6,485
  • 1
  • 25
  • 25