How to check if user typed "may" or "May"? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to check if user typed "may" or "May"?

Console program that will tell the user how many days are in the month they inputted. I can't figure out how to tell if the user typed "may" or "May" & still display the same out come: "May has XX days." with out repeating the same code. Sample Code: string mName; cout << " Enter a month and hit ENTER: \n"; cin >> mName; string mMay, mJun; mMay = "May"; mJun = "June"; if (mName == mMay) { cout << "May has XX days.\n"; } if (mName == mJun) { cout << "June has XX days."; }

19th Jan 2018, 4:51 AM
DragonFly Code
DragonFly Code - avatar
4 Answers
+ 8
instead of adding different cases for user input, try converting user input into a standard format that you can work with. try tolower, it may help. http://www.cplusplus.com/reference/locale/tolower/
19th Jan 2018, 4:57 AM
jay
jay - avatar
+ 6
//try this #include <iostream> #include <string.h> #include <bits/stdc++.h> using namespace std; int main() { string s1="May"; string s2=s1; transform(s2.begin(),s2.end(),s2.begin(),::tolower); if(s1.compare("may")==0||s2.compare("may")==0){ cout<<"may has x no of days"; } return 0; }
19th Jan 2018, 5:40 AM
MR Programmer
MR Programmer - avatar
+ 1
@Kevin Pirritano Your comparison condition in if should be : mMay == "May" || mMay == "may" instead of the one you used earlier if you want to match using OR and ==
19th Jan 2018, 10:21 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
0
The program works when "May" or "June" is typed, but not "may". I tried: mMay = "May" || "may" But that doesn't work.
19th Jan 2018, 4:53 AM
DragonFly Code
DragonFly Code - avatar