+ 2

Why I am not able to take multiple character input in c language I am trying it in vs code.Not ableto take morethan one char inp

https://code.sololearn.com/c64hjeMIhfmp/?ref=app

23rd Jul 2022, 4:24 PM
Suraj Joshi
8 Answers
+ 3
scanf is a booger for many users because it keeps the '\n' char in the input buffer. There are ways to take care of it one is the method Jayakrishna showed using getchar() after a scanf call. Another is to use a space prior to the conversion. char a, b; int c; puts("Enter first char.")" scanf("%a", &a); puts("Enter an interger."); scanf(" %d", &c); puts("Enter a second char."); scanf(" %c", &b); The space gets rid of the white space chars. Another way is to use the suppression conversion %*c such that scanf("%d%*c", &c); ... scanf("%c%*c", &b); Hopefully what I was able to share is using scanf we have to figure out what we will do the remainder of the input buffer and the pesky '\n' char that gets written to strings. Hope to see more code from you soon.
23rd Jul 2022, 10:35 PM
William Owens
William Owens - avatar
+ 2
Try scanf("%d", &c) ; getchar(); scanf("%c", &b) ;
23rd Jul 2022, 4:49 PM
Jayakrishna 🇮🇳
+ 1
How are you giving inputs? Run this code and see output.. #include <stdio.h> int main() { char a,b; int c; printf("first character input"); scanf("%c",&a); printf("first integer input"); scanf("%d",&c); printf("second character input"); scanf("%c",&b); printf("\na=%c c=%d b=%c",a,c,b); } input : a12b Output: is like a = 'a' c = 12 b = 'b'
23rd Jul 2022, 4:36 PM
Jayakrishna 🇮🇳
+ 1
Jayakrishna🇮🇳 visual studio code is skipping second character input.
23rd Jul 2022, 4:44 PM
Suraj Joshi
+ 1
One last look at multiple scanf calls: Here is a snippet with multiple calls that clears the input buffer prior to each subsequent call. https://code.sololearn.com/cFi09ljet1z7
24th Jul 2022, 1:34 AM
William Owens
William Owens - avatar
+ 1
Datatype char is just allow us only one char thats why need to use getchar() ; for add to more char. When we use this no need to add scanf().
24th Jul 2022, 4:52 AM
Nitin Parmar
0
Thanks 🙏
23rd Jul 2022, 4:52 PM
Suraj Joshi
0
William Owens thankyou
24th Jul 2022, 2:01 AM
Suraj Joshi