0
write a program to display name of the month when the user enters the month as a number
its my homework. plz help me and use switch case
8 Antworten
+ 3
Well at least you're honest about the homework part.. you really should be educating yourself on this stuff and understanding the concepts, it will definitely benefit you later on in your course.
#include <iostream>
using namespace std;
int main()
{
int month;
cin >> month;
if (month < 1 || month > 12)
{
cout << "Not a valid month!";
return 0;
}
cout << "Month " << month << " is ";
switch (month)
{
case 1:
cout << "January";
break;
case 2:
cout << "February";
break;
case 3:
cout << "March";
break;
case 4:
cout << "April";
break;
case 5:
cout << "May";
break;
case 6:
cout << "June";
break;
case 7:
cout << "July";
break;
case 8:
cout << "August";
break;
case 9:
cout << "September";
break;
case 10:
cout << "October";
break;
case 11:
cout << "November";
break;
case 12:
cout << "December";
break;
}
}
+ 3
Single quotations are used with character variables. With integer values, you can do without the single quotations.
But you're welcome😁
+ 1
actually i got my mistake
i had put it like
case '1'
+ 1
and thank u
+ 1
Hey guys, why are you doing the homework of others? It defeats the purpose of the homework: OP learns a lot less, if anything.
+ 1
Even though it might sound arrogant after my previous message: it can be done via arrays much shorter.
Maybe doing it via arrays it regained some of the learning effect? 😉
+ 1
I know. I tried to make your homework fulfill its full purpose. :-)
0
it was specified using switch case