0
Why there is recursion if we give the input as logical operator of cases in switch case statements in C ?
#include<stdio.h> int main() { int ch; while(1) { scanf("%d",&ch); /*if user input is 1||2 or 1&&2 it goes in recursion with respect to the first choice*/ switch(ch) { case 1: printf("1\n");break; case 2: printf("2\n");break; case 3: exit(0); break; default: printf("invalid choice\n"); } } return 0; }
28 Answers
+ 3
~ swim ~ Yea, that's what I was implying.
I didn't mean that scanf gets into an infinite loop, just that it was the cause of it getting ... infinite. :)
But I guess you could misinterpret it, after reading it a second time.
+ 3
~ swim ~ I think you are interpreting the question in a different way.
( I think that ) You think that the question is about the fact that it keeps looping regardless of input.
My interpretation is that the program is supposed to loop over and over until a 3 is pressed. But gets in an unresponsive state when invalid input is given ( a char when expecting an integer ).
In your interpretation the while loop is indeed the problem.
In mine, scanf is the problem.
+ 2
I'm pretty sure he's entering "1||2" as input.
In that case when scanf encounters '|' when parsing an int it'll just return while leaving the current input in stdin. Then the loop does its next cycle and reads from the input again which contains the '|' again and that repeats over and over. In which case you're using the wrong term, it's an infinite loop, not recursion.
I don't think there is a portable way to clear the input stream. But my knowledge of C isn't that great :). fflush(stdin) works but that can cause undefined behaviour so not recommended.
Personally I would just go for fgets instead of scanf and then parse the char array with strtol.
So just change scanf to
"
char buffer[50];
fgets( buffer, 50, stdin );
int ch = strtol( buffer, NULL, 10 );
"
+ 2
~ swim ~
Well of course it gets infinite on sololearn, you can only enter input once, unless you enter 3 it'll just hang the next time the input is empty.
Offline fgets waits for input which is exactly its purpose... It's inside an infinite loop after all, taking input after input until the input is a 3 which breaks it.
But it doesn't just stop doing anything like scanf which was the main problem here. So yes, I would say it does solve the problem.
+ 2
@Ulisses Cruz
scanf is just an indirect cause. The entire loops life time depends on the result of scanf, if scanf returns an error and the program doesn't deal with it correctly then there is nothing else that can break it from the loop.
+ 1
Pratik Chavhan can you post an example, please?
+ 1
Pratik Chavhan are you using as input the string "1||2"?
EDITED:
because the scanf is expecting an int.
+ 1
okkk now i got it..
+ 1
Dennis I also don't see how the scanf could cause the loop to become infinite.
0
done.
0
Pratik Chavhan,
after including stdio.h and changing the 'print' to 'printf'
I got no error or recursion.
0
now go on with this code
0
yes , but 1||2 will also evaluate to 1 i.e. it will return an int right?
0
its of dev cpp
0
do u have gcc?...i think it will give same o/p on it also
0
if you give scanf() statement like i have given, what is the o/p then??
0
try this
Why there is recursion if we give the input as logical operator of cases in switch case statements in C ?
#include<stdio.h>
int main()
{
int ch;
while(1)
{
scanf("%d",&ch); /*if user input is 1||2 or 1&&2 it goes in recursion with respect to the first choice*/
switch(ch)
{
case 1:
printf("1\n");break;
case 2:
printf("2\n");break;
case 3:
exit(0); break;
default:
printf("invalid choice\n");
}
}
return 0;
}
0
Ok Dennis I undestand your answer now.