hello guys, is there multiple switch statements in c++? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

15th Aug 2020, 6:59 PM
Muhammad Hussein
Muhammad Hussein - avatar
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; }
17th Aug 2020, 1:59 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
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...
15th Aug 2020, 7:04 PM
Jayakrishna 🇮🇳
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
15th Aug 2020, 7:06 PM
Muhammad Hussein
Muhammad Hussein - avatar
0
It depends on your implementation. I can't able to understand how to relate date printing with switch cases.. How you trying?
15th Aug 2020, 7:17 PM
Jayakrishna 🇮🇳
0
Muhammad Hussein Explain what input needed, and also how the output was expected to be, given such inputs.
15th Aug 2020, 7:37 PM
Ipang
0
lets say he enters 2, 12 and 2020 output would be february 12 2020
15th Aug 2020, 7:39 PM
Muhammad Hussein
Muhammad Hussein - avatar
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 ...
15th Aug 2020, 7:42 PM
Ipang
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",....} ;
15th Aug 2020, 7:46 PM
Jayakrishna 🇮🇳
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
15th Aug 2020, 7:51 PM
Muhammad Hussein
Muhammad Hussein - avatar