0
Why variable b always shows 1?
18 Answers
+ 5
Lucifer Samael Morningstar
because you assign to b the return value of scanf() function, wich occurs after assigning input to b ^^
+ 4
Ipang garbage value?
are you sure to have provided 2 inputs?
+ 4
Visph,
Looks like something was left behind in the input stream after reading string input <a> using fgets(). As you said, it happens when input for <a> was longer than 4 characters. And that leftover data somehow, was accidentally got assigned into <b>.
Input by scanf() works fine if we remove the leftover character as scanf() reads value for <b> in line 8. It appears that this trick only works when input for <a> is 5 characters in length, it won't help when input length for <a> exceeds 5 characters.
Line 8:
scanf( "%*c%ld", &b );
(Edit)
Variable <b> still gets garbage when length of input for <a> exceeds 5 characters, even if we remove the leftover character in the input stream buffer.
+ 3
Lucifer Samael Morningstar
%*c is used to skip exactly one char...
but it only works fine if there remain zero or one char in the input buffer, as the final \n if still in input buffer is automatically skipped ;)
Ipang
the clean solution is a few verbose: testing if \n is in first input... if not, then get char until \n encountered... before taking second output:
int i;
for (i=0; i<5 && a[i]!='\n'; ++i);
if (i<5 && a[i]!='\n') while (getchar()!='\n');
+ 3
Ipang problem is if user enter more than 4 (with your actual code) or 5 (with its solution) character, then second input is not the one expected... because not all input line has been consumed when taking second input...
input buffer is the memory place where is stored the char typed by user... (or in sololearn context the memory place where is stored the string got before sending the script to server ^^)
+ 1
Ipang yes, he did ^^
+ 1
Visph,
Yes of course, first input string "abcde" and second input is whole number (randomly chosen).
I only tested in Code Playground though, haven't checked elsewhere ...
+ 1
Ipang oh, I've only tested with one char as first input... then b show correct value ^^
in fact, as char array of length 5 is used, you get garbage value for b if you provide string of length greater than 4 as first input ;)
+ 1
Ipang Can you add few i/o examples please..
Variable b takes long intigers.. why you're using format specifier '%*c' isn't this for characters?
0
thanks visph
0
visph I have one more question
0
Lucifer,
Did you change the code from its original state when you posted this question?
0
Visph,
Thanks, just asking cause it looks irrelevant with original question. It outputs what seems to be garbage value for variable <b>, not value of 1.
0
visph what's input buffer?
Though second part of the reply was to Ipang, I want to know what is Ipang's problem?
0
visph Oh thanks and what is steam buffer?
0
Lucifer Samael Morningstar
:D
don't know what's "steam" buffer, but "stream" buffer is the memory place where is stored part of a data stream (could be string, as is input buffer) ;)