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."; }
4 Respuestas
+ 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/
+ 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;
}
+ 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 ==
0
The program works when "May" or "June" is typed, but not "may". I tried:
mMay = "May" || "may"
But that doesn't work.