0
hello guys, is there multiple switch statements in c++?
like i want to print the date month and year when user enters those integers
9 Answers
+ 5
Switch case is also use for multiple cases then why u need to write switch cases two or more times in program.
You can also use range in switch case may be this will resolve your problem.
// using range in switch case
#include <stdio.h>
int main()
{
int arr[] = { 1, 5, 15, 20 };
for (int i = 0; i < 4; i++)
{
switch (arr[i])
{
case 1 ... 6:
printf("%d in range 1 to 6\n", arr[i]);
break;
case 19 ... 20:
printf("%d in range 19 to 20\n", arr[i]);
break;
default:
printf("%d not in range\n", arr[i]);
break;
}
}
return 0;
}
0
What is mean by multiple switch statements..
How it relate to description?
You can have Nested switch but multiple means how you want, you can use any number of switch blocks...
0
I mean lets say i want my program to print date, month and year. how many switch statements should i use? be8cause am stuck with the first switch
0
It depends on your implementation. I can't able to understand how to relate date printing with switch cases.. How you trying?
0
Muhammad Hussein
Explain what input needed, and also how the output was expected to be, given such inputs.
0
lets say he enters 2, 12 and 2020
output would be february 12 2020
0
Muhammad Hussein
You can achieve this by using arrays too (if you want). But looking at your output sample, I guess nested switch block is also feasible ...
0
One is enough na?Muhammad Hussein
switch(num)
{
case 1: cout<<"January"; break;
case 2:
....
}
Why need for date, year?
As @Ipang said, you can use array for less line of codes :
string mn[] = {"jan",....} ;
0
because i was trying to do the question and the instruction stated that whenever the user enters those inputs the output should be in that format.. thats why the program looks like it needs nested switch statements though am not sure