-
Character input
Hello everyone. I am new in programming and I wanted to ask your help with a problem. I want to make a programm that uses characters as input (more accurately the days of the week) and according to your decision you get output. (pex if you choose Monday you get 5, if you choose Friday you get 6 etc.). I made a char variable but it doesn;t work in if statement. Please help me because I am desperate :frown:
-
Post the code you tried (that didn't work), and we can tell you why it didn't work (so you learn something and won't repeat the same mistakes again).
-
Here it is
Code:
#include <iostream>
int main()
{
using namespace std;
char day[20];
cout << "Select a day of the week: ";
cin >> day;
if (day == Monday)
{
cout << "Monday is the worst day of the week!";
}
else if (day == Tuesday)
{
cout << "Tuesday is boring";
}
else if (day == Wednesday);
{
cout << "Wednesday is interesting";
}
else if (day == Thursday)
{
cout << "Thursday is fine";
}
else if (day == Friday)
{
cout << "Friday is great!";
}
else if (day == Saturday)
{
cout << "Night out";
}
else
{
cout << "Relaxing";
}
return 0;
}
IT SAYS WARNING ON IF STATEMENT AND DOESN'T COMPILE
-
You need " "
As in
if (day == "Monday")
Also, you're using char arrays, not std::string, so you also need
if ( strcmp(day,"Monday") == 0 )
-
Change the type of day to std::string. This way of reading input is unsafe.