So, I'm trying to make a code so, it asks the user what day it is, and the user says a response depending on the day (I'm very bad at code don't judge me)
Code:
string day;
cout <<
"please input current day \n(monday, tuesday, wednsday, thursday, friday)";
cin >> day;
if (day == "monday")
{
cout << "I HATE monday";
}
if else
(day == "tuesday")
{
cout << "almost halfway, but tuesday";
}
if else
(day == "wednsday")
{
cout << "Is it wednsday";
}
if else
(day == "thursday")
{
cout << "almost friday but thursday";
}
if else
(day == "friday")
{
cout << "yay friday";
}
if else
{
cout
<<"how did you get that wrong?!??!?!?";
}
if (expression_1) {
// ... code to be executed of expression_1 holds true
} elseif (expression_2) {
// ... code to be executed of expression_2 holds true (and none of the previous checks was true)
} elseif (expression_3) {
// ... code to be executed of expression_3 holds true (and none of the previous checks was true)
} else {
// ... code to be executed if *none* of the previous checks was true
}
Also, for string comparison you have to use:
1 2 3
if (strcmp(day, "monday") == 0) {
// ... code to execute if the string pointed to by 'day' equals "monday"
}
You can use the == operator to test for equality of std::string objects, but it does not work with plain C strings!
BTW: If you want case-insensitive string comparison, use stricmp() instead of strcmp().
C++ string comparisons have to be exact in case as well as letter layout. "Friday" is not "friday", is not "firday." "wednsday" is also a possible input error waiting to happen.
Strings are notorious for input failures with users. There are ways and/or library functions to eliminate case issues so no matter what the user enters you have to make only one comparison for a given day.
You might consider using a numeric input instead, with a prompt for the numeric input.