473,405 Members | 2,421 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

switch case syntax

36
if i want right after performed case (number): {.......}
go back to the menu .

why the prog goes on to case 2 after perform case 1 ?

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #define size 50 //max
  3. void main()
  4. {
  5.     int sel, cols, rows, i, j, arr[size][size]={0} ;
  6.     printf("enter rows and cols [1..50] ==>");
  7.     scanf("%d",&cols);
  8.     scanf("%d",&rows);
  9.     printf("1. display the matrix\n"
  10.            "2. input values to matrix\n"
  11.            "3. transpose matrix\n"
  12.            "4. i-th power\n"
  13.            "5. sort the matrix by rows sum\n"
  14.            "6. find sub matrices\n"
  15.            "7. long addition\n"
  16.            "0. quit\n");
  17.     scanf("%d",&sel);
  18.     switch(sel)
  19.     {
  20.     case 1:
  21.         for(i=0; i<cols; i++)
  22.         {
  23.             printf("\n");
  24.             for(j=0; j<rows; j++)
  25.                 printf("%d",arr[i][j]);
  26.         }
  27.     case 2:
  28.         printf("enter data for %dx%d ==>",rows,cols);
  29.  
  30.     }
  31.     getchar();
  32.  
  33. }
  34.  
  35.  
  36.  
Aug 16 '09 #1
4 4669
weaknessforcats
9,208 Expert Mod 8TB
That is a feature of the switch statement.

If you want just one case to execute, you need a break statement at the end of the case:

Expand|Select|Wrap|Line Numbers
  1. case 1:
  2.             //logic here
  3.             break;
The fall-through features lets you use the same logic for several cases:

Expand|Select|Wrap|Line Numbers
  1. case Jan:
  2. case Mar:
  3. case May:
  4. case Jul:
  5. case Aug:
  6. case Oct:
  7. case Dec:
  8.         NumberOfDays = 31;
  9.         break;
Aug 16 '09 #2
sedaw
36
ok... but after the break i want to go back to main menu
and now let the user select another case (another number)
what bout this:
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #define size 50 //max
  3. void main()
  4. {
  5.     int sel=-1, cols, rows, i, j, arr[size][size]={0} ;
  6.     printf("enter rows and cols [1..50] ==>");
  7.     scanf("%d",&cols);
  8.     scanf("%d",&rows);
  9.     switch(sel)
  10.     {
  11.     case 1:
  12.         for(i=0; i<cols; i++)
  13.             for(j=0; j<rows; j++)
  14.                 printf("%d",arr[i][j]);
  15.             break;
  16.     default:
  17.     printf("1. display the matrix\n"
  18.            "2. input values to matrix\n"
  19.            "3. transpose matrix\n"
  20.            "4. i-th power\n"
  21.            "5. sort the matrix by rows sum\n"
  22.            "6. find sub matrices\n"
  23.            "7. long addition\n"
  24.            "0. quit\n");
  25.     scanf("%d",&sel);
  26.     }
  27.     getchar();
  28. }
  29.  
still not workin`

TNX .............
Aug 16 '09 #3
weaknessforcats
9,208 Expert Mod 8TB
That's because your scanf is part of the default case.

Remember, the switch is just a series if if-else logic blocks. As soon as you break, you have left the switch statement.

To go back and scan another choice you need a loop with the switch inside the loop:

Expand|Select|Wrap|Line Numbers
  1. while (1)
  2. {
  3.      scanf(choice);
  4.      switch (choice)
  5.      {
  6.  
  7.      }
  8. }
In this setup you stay in the loop forever. When you scanf to quit, you need to leave the function containing the loop. Like this:

Expand|Select|Wrap|Line Numbers
  1. case quit:
  2.           //do the cleanup here
  3.           return;   
That will return from the function containing the while loop. If the while loop is in your main(), this is the return from main().
Aug 16 '09 #4
sedaw
36
thank you very much!
you helped me alot (-:
Aug 16 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Myster Ious | last post by:
Polymorphism replaces switch statements, making the code more compact/readable/maintainable/OO whatever, fine! What I understand, that needs to be done at the programming level, is this: a...
21
by: Andy | last post by:
Can someone tell me if the following Switch...Case construct is valid? I'm wanting to check for multiple values in the Case statement without explicitly listing each values. So for example, will...
10
by: Chih-Hsu Yen | last post by:
I encountered a strange problem about switch-case statement. switch(cmd) { case 1: statements; break; case 2: statements; break; ... .... case 11: S1; S2; S3; statements;
15
by: Benny Raymond | last post by:
I'm confused as to how fallthrough is limited in switch. For example the following works: string switch_test = "a"; switch (switch_test) { case "a": case "b": case "c": doSomething(a);
13
by: Fei Liu | last post by:
Hi Group, I've got a problem I couldn't find a good solution. I am working with scientific data files in netCDF format. One of the properties of netCDF data is that the actual type of data is only...
5
by: sam_cit | last post by:
Hi Everyone, I read somewhere that there are some compile time operations behind switch-case, which is why it can work for cases which evaluates to an integer or character and not strings and...
9
by: sam_cit | last post by:
Hi Everyone, I wanted to know as to how a switch case like the following one is converted into a jump table by the compiler, assuming that it does, switch(i) { case 4 : { ... printf("4");
6
by: asit | last post by:
please modify to get the correct output.(switch case is compulsory) #include <stdio.h> int main() { char ch; printf("Enter any character : "); ch=getch(); switch(ch)
13
by: Satya | last post by:
Hi everyone, This is the first time iam posting excuse me if iam making any mistake. My question is iam using a switch case statement in which i have around 100 case statements to compare. so...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.