#include <iostream>
#include <string>
int main()
{
usingnamespace std;
string today;
cout << "What day of the week is it? ";
cin >> today;
if (today == "Sunday")
cout << "today is Sunday";
if (today == "Monday")
cout << "today is Monday";
if (today == "Tuesday")
cout << "today is Tueday";
if (today == "Wednesday")
cout << "today is Wednesday";
if (today == "Thursday")
cout << "today is Thursday";
if (today == "Friday")
cout << "today is Friday";
if (today == "Saturday")
cout << "today is Saturday";
else
cout << "you have entered the wrong input, please try again";
}
Why does it mesh a day of the week if today is true?
#include <iostream>
#include <string>
usingnamespace std;
int main(){
string today;
cout << "What day of the week is it?" << endl;
cin >> today;
if (today == "Sunday")
cout << "today is Sunday";
elseif (today == "Monday")
cout << "today is Monday";
elseif (today == "Tuesday")
cout << "today is Tueday";
elseif (today == "Wednesday")
cout << "today is Wednesday";
elseif (today == "Thursday")
cout << "today is Thursday";
elseif (today == "Friday")
cout << "today is Friday";
elseif (today == "Saturday")
cout << "today is Saturday";
else
cout << "you have entered the wrong input, please try again";
return 0;
}
Here's my guess as to what happened:
In your example there are a bunch of ifs and one else. The else "links" itself with one of the ifs (say Saturday). So when you enter a valid day (say monday), one of the if conditions is fulfilled, HOWEVER, when it checks for saturday, the condition is not fulfilled, so it automatically goes to the else.
In the multiway if statement, the else is only ever executed when all of the other ifs are not fulfilled.
i am confused at why this works
i am very early in learning c++, is this something i missed learning or is it something deeper in that i will learn later?