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 ? - #include <stdio.h>
-
#define size 50 //max
-
void main()
-
{
-
int sel, cols, rows, i, j, arr[size][size]={0} ;
-
printf("enter rows and cols [1..50] ==>");
-
scanf("%d",&cols);
-
scanf("%d",&rows);
-
printf("1. display the matrix\n"
-
"2. input values to matrix\n"
-
"3. transpose matrix\n"
-
"4. i-th power\n"
-
"5. sort the matrix by rows sum\n"
-
"6. find sub matrices\n"
-
"7. long addition\n"
-
"0. quit\n");
-
scanf("%d",&sel);
-
switch(sel)
-
{
-
case 1:
-
for(i=0; i<cols; i++)
-
{
-
printf("\n");
-
for(j=0; j<rows; j++)
-
printf("%d",arr[i][j]);
-
}
-
case 2:
-
printf("enter data for %dx%d ==>",rows,cols);
-
-
}
-
getchar();
-
-
}
-
-
-
4 4669
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: - case 1:
-
//logic here
-
break;
The fall-through features lets you use the same logic for several cases: - case Jan:
-
case Mar:
-
case May:
-
case Jul:
-
case Aug:
-
case Oct:
-
case Dec:
-
NumberOfDays = 31;
-
break;
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: - #include <stdio.h>
-
#define size 50 //max
-
void main()
-
{
-
int sel=-1, cols, rows, i, j, arr[size][size]={0} ;
-
printf("enter rows and cols [1..50] ==>");
-
scanf("%d",&cols);
-
scanf("%d",&rows);
-
switch(sel)
-
{
-
case 1:
-
for(i=0; i<cols; i++)
-
for(j=0; j<rows; j++)
-
printf("%d",arr[i][j]);
-
break;
-
default:
-
printf("1. display the matrix\n"
-
"2. input values to matrix\n"
-
"3. transpose matrix\n"
-
"4. i-th power\n"
-
"5. sort the matrix by rows sum\n"
-
"6. find sub matrices\n"
-
"7. long addition\n"
-
"0. quit\n");
-
scanf("%d",&sel);
-
}
-
getchar();
-
}
-
still not workin`
TNX .............
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: - while (1)
-
{
-
scanf(choice);
-
switch (choice)
-
{
-
-
}
-
}
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: - case quit:
-
//do the cleanup here
-
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().
thank you very much!
you helped me alot (-:
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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;
|
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);
|
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...
|
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...
|
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");
|
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)
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
| |