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; }

11th Mar 2019, 6:11 PM
Avatar
Avatar - avatar
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.
11th Mar 2019, 7:25 PM
Dennis
Dennis - avatar
+ 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.
11th Mar 2019, 8:03 PM
Dennis
Dennis - avatar
+ 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 ); "
11th Mar 2019, 7:11 PM
Dennis
Dennis - avatar
+ 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.
11th Mar 2019, 7:56 PM
Dennis
Dennis - avatar
+ 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.
11th Mar 2019, 8:10 PM
Dennis
Dennis - avatar
+ 1
Pratik Chavhan can you post an example, please?
11th Mar 2019, 6:22 PM
Ulisses Cruz
Ulisses Cruz - avatar
+ 1
Pratik Chavhan are you using as input the string "1||2"? EDITED: because the scanf is expecting an int.
11th Mar 2019, 6:46 PM
Ulisses Cruz
Ulisses Cruz - avatar
+ 1
okkk now i got it..
11th Mar 2019, 7:13 PM
Avatar
Avatar - avatar
+ 1
Dennis I also don't see how the scanf could cause the loop to become infinite.
11th Mar 2019, 8:04 PM
Ulisses Cruz
Ulisses Cruz - avatar
0
done.
11th Mar 2019, 6:30 PM
Avatar
Avatar - avatar
0
Pratik Chavhan, after including stdio.h and changing the 'print' to 'printf' I got no error or recursion.
11th Mar 2019, 6:37 PM
Ulisses Cruz
Ulisses Cruz - avatar
0
now go on with this code
11th Mar 2019, 6:42 PM
Avatar
Avatar - avatar
0
yes , but 1||2 will also evaluate to 1 i.e. it will return an int right?
11th Mar 2019, 6:46 PM
Avatar
Avatar - avatar
0
its of dev cpp
11th Mar 2019, 6:51 PM
Avatar
Avatar - avatar
0
do u have gcc?...i think it will give same o/p on it also
11th Mar 2019, 6:54 PM
Avatar
Avatar - avatar
0
if you give scanf() statement like i have given, what is the o/p then??
11th Mar 2019, 6:57 PM
Avatar
Avatar - avatar
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; }
11th Mar 2019, 7:04 PM
Avatar
Avatar - avatar
0
Ok Dennis I undestand your answer now.
11th Mar 2019, 8:10 PM
Ulisses Cruz
Ulisses Cruz - avatar