0

C "int" input problem

Yo! I have created a program which will take "int" input from user using "scanf". It has 3 parts. If I accidentally input a "character" in 1st part, then my program skips 2nd and 3rd part of taking input. And I'm not doing this in SoloLearn. But in SoloLearn, output is same. It will not read your 2nd and 3rd input if you input a "character" in 1st part. Please Help! What i should do? How to prevent users from inputting "Character"?? Why this is happening? https://code.sololearn.com/c22Nes35Q99D/?ref=app

3rd Feb 2021, 8:03 AM
🌀 Shail Murtaza شعیل مرتضیٰ
🌀 Shail Murtaza شعیل مرتضیٰ - avatar
8 ответов
+ 2
Cyan Shahzad If any character will not match with format specifier of scanf function then scanf will not take that input. As input remains in buffer that makes scanf to skip taking new input. So I will have to flush buffer before taking new input from scanf. For flushing I used ungetc and with getch I take character as input. If input will be digit then I just read buffer with scanf function.
3rd Feb 2021, 5:59 PM
🌀 Shail Murtaza شعیل مرتضیٰ
🌀 Shail Murtaza شعیل مرتضیٰ - avatar
+ 4
Press 'enter key', or go to next line to input to the next variable(s) as input values. _______________________________ "Looks like your program needs input" _______________________________ 123 12 42 _______________________________ The reason is that, SoloLearn only takes input from user only once. In other words, the console in code playground is not interactive, that is why you have to input values at once, new line per 'scanf'.
3rd Feb 2021, 8:09 AM
noteve
noteve - avatar
+ 2
Cyan I guess you didn't read my question carefully. And I already told in description that output is same in SoloLearn. I already now what you are talking about.
3rd Feb 2021, 8:32 AM
🌀 Shail Murtaza شعیل مرتضیٰ
🌀 Shail Murtaza شعیل مرتضیٰ - avatar
+ 2
That is because all of your 3 parts are using the same variable/address "n", try to change each part into different variables (e.g. x,y,z) Although the compiler will generate some weird numbers (i forgot what those are), if you have one or two inputs missing. https://code.sololearn.com/c9A6A11A171A
3rd Feb 2021, 8:41 AM
noteve
noteve - avatar
+ 1
Shahzad I modified your program a little. You were using multiple variable names without any reason, just waste of memory You were using exit() function. You can also end your program by return key word. https://code.sololearn.com/cPJ8R8K42T0K/?ref=app
4th Feb 2021, 9:42 AM
🌀 Shail Murtaza شعیل مرتضیٰ
🌀 Shail Murtaza شعیل مرتضیٰ - avatar
+ 1
When a non-digit character is entered, the first scanf() aborts the read cycle and stores the default value 0 in n. Since the first character is not being consumed by scanf(), this happens to the rest. To avoid this, try getting the input in the character form and subtract 48 from it, in order to get the integer.
22nd Apr 2021, 6:55 PM
Calvin Thomas
Calvin Thomas - avatar
+ 1
Calvin Thomas Ok Thanks! I will try that out
22nd Apr 2021, 6:59 PM
🌀 Shail Murtaza شعیل مرتضیٰ
🌀 Shail Murtaza شعیل مرتضیٰ - avatar
0
Shahzad Thanks bro but that didn't work
3rd Feb 2021, 9:17 AM
🌀 Shail Murtaza شعیل مرتضیٰ
🌀 Shail Murtaza شعیل مرتضیٰ - avatar