+ 2

C scanf() question (no. 2) [SOLVED]

Consider this code: https://code.sololearn.com/cqsDiKu82DKf/?ref=app Why is the newline character still there after giving a 2-line input? Shouldn't it be consumed by "%*c" ?

20th Apr 2021, 7:52 AM
Calvin Thomas
Calvin Thomas - avatar
5 Réponses
+ 5
I don't think you need the 's' after the set. It will try to match a corresponding 's' in the input stream, instead of belonging to the set, and therefore cause scanf() to abort early when encountered: https://stackoverflow.com/questions/33971044/scanf-limited-character-set
20th Apr 2021, 8:32 AM
Shadow
Shadow - avatar
+ 3
Calvin, Shadow is right, so instead of scanf("%[^\n]s %*c %s", a, b); You can try scanf("%[^\n]%*c %s", a, b); Tried it, and seems to be working fine
20th Apr 2021, 8:35 AM
Ipang
+ 2
Do you give name inputs on single line or in separate line?
20th Apr 2021, 8:24 AM
Ipang
+ 1
Ipang It's separate.
20th Apr 2021, 8:32 AM
Calvin Thomas
Calvin Thomas - avatar
+ 1
Shadow and Ipang Thanks a lot! That makes sense; I had thought that the inclusion-exclusion set required an 's'.
20th Apr 2021, 8:40 AM
Calvin Thomas
Calvin Thomas - avatar