I'm a beginner at C++ and I am trying to prompt the user to enter the month and year in the format XX/XX an print the written date, day, and season. Although, I can't figure out how to enter the values in the specific format XX/XX in one line. For example, 01/22 for January 22. Also, I'm using a switch statement since it's the focus of the chapter I'm working on so I would like to keep it that way.
#include <iostream>
using namespace std;
int main()
{
int month, day;
cout << "Enter the date in XX/XX: \n";
cin >> month;// Using a "space bar" instead of "enter" prevents the new line in cin
cout << "/";
cin >> day;
switch (month)
{ // The integers don't accept the single quotes '1'
case 1: cout << "The date entered is January " << day << ". The season is winter.\n0"; break;
case 2: cout << "The date entered is February " << day << ". The season is spring.\n"; break;
case 3: cout << "The date entered is March " << day << ". The season is spring.\n"; break;
case 4: cout << "The date entered is April " << day << ". The season is spring.\n"; break;
case 5: cout << "The date entered is May " << day << ". The season is spring.\n"; break;
case 6: cout << "The date entered is June " << day << ". The season is summer.\n"; break;
case 7: cout << "The date entered is July " << day << ". The season is summer.\n"; break;
case 8: cout << "The date entered is August " << day << ". The season is summer.\n"; break;
case 9: cout << "The date entered is September " << day << ". The season is fall.\n"; break;
case 10: cout << "The date entered is October " << day << ". The season is fall.\n"; break;
case 11: cout << "The date entered is November " << day << ". The season is winter.\n"; break;
case 12: cout << "The date entered is December " << day << ". The season is winter.\n"; break;
default: cout << "The input is invalid.\n" << endl; break;
}
return 0;
}
I tried writing
cin >> month >> day;
but I didn't receive what I wanted by entering in the format I wanted, 01/22.
I also tried
cin >> month;
cout << "/";'
cin >> day;
but that required me to press the enter key and then it created a new line.