When you have a list of cases in a switch function can you nest ifs and things of that nature in a single case?
When you have a list of cases in a switch function can you nest ifs and things of that nature in a single case?
Sure. You can have switch statements with if, while, for, do-while, function calls, local variable declarations, or any other piece of C that you could put inside a function in C in general.
It is, however, advisable to keep the code in the case short, preferrably just calling a function that does the real work.
If you use local variables in a specific case, make sure you put braces around it:
--Code:... switch(x) { case 1: ... break; case 2: { int y; y = 4; while (y < z) y << 2; ... } break; ....
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Alright, I nested some ifs else ifs into the switch, which isn't a short series like you advised but I don't know how else to do it.
The program I'm writing allows the user to input a two digit number and it prints out the english word for the number.
I can get all cases to work except for 11-19, which prints the work, in addition to the second digit. For example, if you input 19, it prints:
ninteen
nine
I know the problem is the first switch is working fine but when the second switch isn't necessary, its printing anyway. I can't figure out how to ignore the second switch if it isn't required.
Any ideas?Code:// This program takes a two digit number from the user and // prints the English word for the number #include <stdio.h> main (void) { int num, first, second; printf("Enter a two-digit number: "); scanf("%d", &num); if (num < 10 || num > 99) { printf("Incorrect input: Please enter a two-digit number \n"); } else { first = num / 10; second = num%10; switch (first) { case 9: printf("ninety"); break; case 8: printf("eighty"); break; case 7: printf("seventy"); break; case 6: printf("sixty"); break; case 5: printf("fifty"); break; case 4: printf("forty"); break; case 3: printf("thirty"); break; case 2: printf("twenty"); break; case 1: if (first == 1 && second == 0) { printf("ten \n"); } else if (first == 1 && second == 1) { printf("eleven \n"); } else if (first == 1 && second == 2) { printf("twelve \n"); } else if (first == 1 && second == 3) { printf("thirteen \n"); } else if (first == 1 && second == 4) { printf("fourteen \n"); } else if (first == 1 && second == 5) { printf("fifteen \n"); } else if (first == 1 && second == 6) { printf("sixteen \n"); } else if (first == 1 && second == 7) { printf("seventeen \n"); } else if (first == 1 && second == 8) { printf("eighteen \n"); } else if (first == 1 && second == 9) { printf("nineteen \n"); } break; } switch (second) { case 9: printf("-nine \n"); break; case 8: printf("-eight \n"); break; case 7: printf("-seven \n"); break; case 6: printf("-six \n"); break; case 5: printf("-five \n"); break; case 4: printf("-four \n"); break; case 3: printf("-three \n"); break; case 2: printf("-two \n"); break; case 1: printf("-one \n"); break; } } return 0; }
You need to have some sort of if-statement around (or inside) your second switch. I thinkwould do.Code:if (first != 1) ...
Of course, you should probably use a switch inside case 1 in the first switch statement - and you definitely don't need thesections, because you are in the "1" case for "first", so no other value should end up here.Code:... first == 1 && ...
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
That works perfect, thanks man. That was driving me nuts.
Looks like a long winded way of implementing an array to me.
Unless switch/case was the point of the exercise.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.