"break; " not working in c in MANDRIVA

in programs of switch-case ,etc .
break; is not executing .
program does not give any errors .......but break command is not executed .

case 5 :
printf("array is :- ");
for(i=0;i<n;i++)
printf("%d",a);
break;

^ it's just a case for displaying array ..........when 5 is selected ........array goes on displaying infinete no. of time......it does not stops ....i.e break is not executed .

i code directly through console in MANDRIVA .
is there need for header file other than <stdio.h> ?
OR i am missing some libraries ?
 
Sudarshan_SMD said:
in programs of switch-case ,etc .
break; is not executing .
program does not give any errors .......but break command is not executed .

case 5 :
printf("array is :- ");
for(i=0;i<n;i++)
printf("%d",a);
break;

^ it's just a case for displaying array ..........when 5 is selected ........array goes on displaying infinete no. of time......it does not stops ....i.e break is not executed .

i code directly through console in MANDRIVA .
is there need for header file other than <stdio.h> ?
OR i am missing some libraries ?


Read the code carefully mate. You will figure out why.

~LT
 
linuxtechie said:
Read the code carefully mate. You will figure out why.

~LT

plz give direct suggestions .......if i can't figure it out

case 5 :

printf("array is :- ");

for(i=0;i<n;i++)

{

printf("%d",a);

}

break;

this also not works .

here n = no of elements in array .

----------------------------------------------------------------

here is whole program

program is to make user defined array & then make operations on it .................

in each case here break is not executed .

#include<stdio.h>

main()

{

int a[20],i,j,n,s,pos,no;

printf("enter number of elements you want in an array- ");

scanf("%d",&n);
printf("enter the elements of array");

for(i=0;i<n;i++)

scanf("%d",&a);
printf("you entered array is:- ");

for(i=0;i<n;i++)

printf("%d",a);

printf(" which operation would you like to perform ? /n 1:insert number at selected position /n 2:delete an number /n 3:find frequency of a number /n 4:display reversed array /n 5:display he array /n 6: exit ");

scanf("%d",&s);

do{

switch(s)

{

case 1 :

printf("at which position you want ti insert number?");

scanf("%d",&pos);

printf("enter number you want to inser");

scanf("%d",&no);

for(i=pos;i<=n-1;i++){

a=a[i+1];

}

a[pos]=no;

break;

case 2 :

printf("enter the number to be deleted:");

scanf("%d",&no);

for(i=0;i<=n-1;i++)

{

if(a==no)

{

for(j=pos;j<=n;j++)

a=a[i-1];

printf("%d is deleted ",no);

}

}

break;

case 3 :

j=0;

printf("enter number whose frequency is to be measured");

scanf("%d",no);

for(i=0;i<=n-1;i++)

{

if(a==no){

j++;

}

}

printf("frequency of number %d is %d" ,no,j);

break;

case 4 :

printf("revrse of array is :- ");

for(i=n-1;i>=0;i--)

printf("%d",a);

break;

case 5 :

printf("array is :- ");

for(i=0;i<n;i++)

{

printf("%d",a);

}

break;
case 6 :

break;

default:

printf("enter a vaild option");

break;

}

}while(s!=6);

}
 
Sudarshan_SMD said:
it's just a case for displaying array ..........when 5 is selected ........array goes on displaying infinete no. of time......it does not stops ....i.e break is not executed .
the break in your code is not going to affect the 'for loop' in any way. the only way it can continue displaying infinitely is due to some extremely large value for 'n'

can you post the full code or atleast the function which has the 'switch-case'
which is causing the problem so that we can try to find the problem.
alternatively you can try running the code in windows if possible and see if there is any difference.
 
pr0ing said:
the break in your code is not going to affect the 'for loop' in any way. the only way it can continue displaying infinitely is due to some extremely large value for 'n'

can you post the full code or atleast the function which has the 'switch-case'

which is causing the problem so that we can try to find the problem.

alternatively you can try running the code in windows if possible and see if there is any difference.

n i entered is not greater than5 .........so t must stop after sidplaying that much elements .

the code is probably is working in TURBOC with #include<stdio.h>

-------------

if i give other option than 1-6

then default will execute OK.

but then defaultalso continusly displys infinite no. of times.......it does not breaks

not tried by own on TYURBO C
 
@Sudarshan_SMD

i tried the code and it is running properly.

i.e. the break statements are being executed properly.

as linuxtechie has already pointed out.
plese check your code as it has a minor flaw. some misplaced statements.

if you try single-stepping the code it will be easy to find out the problem.
if its not possible to single-step try using 'printf' statements at various points in the code to identify what is wrong.
 
Sudarshan_SMD said:
plz give direct suggestions .......if i can't figure it out

case 5 :

printf("array is :- ");

for(i=0;i<n;i++)

{

printf("%d",a);

}

break;

this also not works .



:mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad:

Mods: Pardon me, but nothing else could explain it any better.

~LT
 
linuxtechie said:
:mad: :mad: :mad:

Mods: Pardon me, but nothing else could explain it any better.
~LT

cut the guy some slack. he reminds me of a lot of my ex-college mates.
Sudarshan_SMD

try adding 'printf' statements everywhere in the code to find out how "do-while", "switch-case", "break" exactly works.
 
hey.......i am not able to figure out the problem here .

in above code .......only if i choose 6 as option i.e to exit .........it works .

means do while loop is executing .

but for other options i choose it goes on eecuting infinie no of times even if break; is present .

EDIT:

is it problem with libraries ?

that mine linux console do not recognize break; ???????

my frnd. told that use gedit command in console to specify program to c

by when i type $ gedit ***.c

it says bash: gedit command not found ........

so is it problem with libraries ?????
 
No problem with C.

Its a human error. Its a simple problem with your code. We would rather have you figure it out rather than point it out to you. That way you learn something ;)

Try debugging it, you will see why break doesnt get executed.
 
Sudarshan_SMD said:
hey.......i am not able to figure out the problem here .

in above code .......only if i choose 6 as option i.e to exit .........it works .

means do while loop is executing .

but for other options i choose it goes on eecuting infinie no of times even if break; is present .

EDIT:

is it problem with libraries ?

that mine linux console do not recognize break; ???????

my frnd. told that use gedit command in console to specify program to c

by when i type $ gedit ***.c

it says bash: gedit command not found ........

so is it problem with libraries ?????

why don't use a IDE like geany to write your code?
 
techie_007 said:
No problem with C.

Its a human error. Its a simple problem with your code. We would rather have you figure it out rather than point it out to you. That way you learn something ;)

Try debugging it, you will see why break doesnt get executed.

after i give $ cc****.c

then theres no error .........which occours when program has some bugs .

----------------------------------------------------------------------------

thank you every body for helpin.

at last figured out the problem .......

actually break; was executing but due to condition /placemnt of do while loop it was

no its working

code is ,

sorry for any nonsence .........i am new to C

#include<stdio.h>

main()

{

int a[20],i,j,n,s,pos,no;

printf("enter number of elements you want in an array- ");

scanf("%d",&n);

printf("enter the elements of array");

for(i=0;i<n;i++)

scanf("%d",&a);

printf("you entered array is:- ");

for(i=0;i<n;i++)

printf("%d",a);

while(1)

{

printf(" which operation would you like to perform ? /n 1:insert number at selected position /n 2:delete an number /n 3:find frequency of a number /n 4:display reversed array /n 5:display he array /n 6: exit ");

scanf("%d",&s);

switch(s)

{

case 1 :

printf("at which position you want ti insert number?");

scanf("%d",&pos);

printf("enter number you want to inser");

scanf("%d",&no);

for(i=pos;i<=n-1;i++){

a=a[i+1];

}

a[pos]=no;

break;

case 2 :

printf("enter the number to be deleted:");

scanf("%d",&no);

for(i=0;i<=n-1;i++)

{

if(a==no)

{

for(j=pos;j<=n;j++)

a=a[i-1];

printf("%d is deleted ",no);

}

}

break;

case 3 :

j=0;

printf("enter number whose frequency is to be measured");

scanf("%d",no);

for(i=0;i<=n-1;i++)

{

if(a==no){

j++;

}

}

printf("frequency of number %d is %d" ,no,j);

break;

case 4 :

printf("revrse of array is :- ");

for(i=n-1;i>=0;i--)

printf("%d",a);

break;

case 5:

printf("array is :- ");

for(i=0;i<n;i++)

{

printf("%d",a);

}

break;
case 6 :

break;

default:

{

printf("enter a vaild option");

break;

}

}

}

}

only problem is i cant get out if i press 6 .....its is obvious as per the code .

what should i use in case 6 : to exit the program ?

tried exit; but not working
 
Back
Top