0

Can anyone point out why the code won't work??

https://code.sololearn.com/cAu2RQ1hzRAK/?ref=app the loop is supposed to execute till the scanf() reads an integer .But weirdly,once it reads a non integer value it goes into an infinite loop without executing scanf() again.

28th Jul 2018, 9:00 AM
THE GAMER
THE GAMER - avatar
10 ответов
+ 4
int n; char chk; if(scanf("%d%c", &n, &chk) != 2 || chk != '\n') printf("Invalid Input"); else printf("valid input"); this is a greedy method to test the input but, i haven't tried it in loop, try this if it helps, if this won't workout, then in my knowledge there's no other way in C
28th Jul 2018, 10:43 AM
Nikhil Dhama
Nikhil Dhama - avatar
+ 2
You can't do anything to solve this, for int type variable user is supposed input an integer value but if you'll enter any character then it will assign a garage value not only for this variable, but for all upcoming inputs(scanf), (irrespective of their type) that's why it's going into an infinite loop
28th Jul 2018, 10:25 AM
Nikhil Dhama
Nikhil Dhama - avatar
+ 2
For c, it's bit lengthy... read input as string... try to convert the same into integer and just come to know whether input was int or not based on conversion status... below is the easiest way in c++ to achieve the same: int a; cin >> a; if(cin.fails()) { // your code when user entered value other than int }
28th Jul 2018, 10:53 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 2
in your updated code, scanf is culprit... replace with line: scanf("%c",&i); just note that sololearn doesn't take input interactively... all input to be given by space seperated in single go... for example, w 2 or 3.. I checked with above two input and it works with a single line update in your code
28th Jul 2018, 11:42 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
@Nikhil Dhama:so, are you saying that input verification is not at all possible in c??😢
28th Jul 2018, 10:36 AM
THE GAMER
THE GAMER - avatar
+ 1
thanks,will try👍
28th Jul 2018, 10:45 AM
THE GAMER
THE GAMER - avatar
0
unfortunately, scanf returns 1 if input was "23s"...
28th Jul 2018, 10:55 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
#include <stdio.h> //for single digit int int main() { char i; do { scanf("%c",i); if((i-48)>9||(i-48)<0) printf("invalid input\n"); //checks if i is integer }while((i-48)>9||(i-48)<0);//loop "should"work until scanf() reads an integer printf("%c",i); return 0; } still the same output!!😢
28th Jul 2018, 11:33 AM
THE GAMER
THE GAMER - avatar
0
the thing is I can check if the input is integer or not but I can't prompt the user to give another value in the same execution!!
28th Jul 2018, 11:36 AM
THE GAMER
THE GAMER - avatar
0
sry,that was an accident!😅(&i).And the problem still exist in an interactive environment.i.e.,if you give the first input as a non integer value, it won't wait for user to enter the next value when control reaches the scanf() (or get char()) ,it just takes the next value in buffer. input :error5 output: invalid input invalid input invalid input invalid input invalid input 5 (five "invalid input" for each letter in error,even in interactive input mechanism!!)
28th Jul 2018, 12:19 PM
THE GAMER
THE GAMER - avatar