You are on page 1of 4
‘W972, 10:41 AM C: Multiple scants, when | antrin a value for one scarf skips the secand scant - Slack Overlow C: Multiple scanf's, when | enter in a value for one scanf it skips the second scanf [duplicate] Asked 9 years, 8 months ago Active 3 years, 7 months ago Viewed 103k times This question already has answers here: 21 scanfll leaves the new line char in the buffer (5 answers) Closed 4 years ago, ta have this block of code (functions omitted as the logic is part of a homework assignment): include int main() { char ¢ int size; print #("\nshape (1/s/t):")5 scanf("%e" 8c) 5 print #(“Length:"); scanf("Xd" size); white(c!="q") { switeh(c) « case '1': Line(size); break; case's’: square(size); break; case 't': triangle(size); break; x nshape (1/s/t):")5 * 8); y return @; The first two Scanf's work great, no problem once we get into the while loop, | have a problem where, when you are supposed to be prompted to enter a new shape char, it instead jumps down to the printf of Length and waits to take input from there for a char, then later a decimal on the next iteration of the loop. Preloop iteration: Scanf: Shape. Works Great Scanf: Length. No Problem hitps:lstackoverfiow.comquestions'95622 18/c-multiple-scan's-wher--enter-in-a-alue-for-one-scant-i-skips-the-second-s wa ‘W972, 10:41 AM C: Multiple scants, when | antrin a value for one scarf skips the secand scant - Slack Overlow Loop 1. Scanf: Shape. Skips over this Scanf: length. Problem, this scanf maps to the shape char. Loop 2 Scant: Shape. Skips over this Scanf: length, Problem, this scanf maps to the size int now. Why is it doing this? © seant Share Improve this question edited Oct 314 at 14:40 asked Mar 512 at 5:54 Follow Fst Jonathan Leffler BETE snow Mac 681k 128 834 1204 Sia 5359 16 52 79 7 Answers Active | Oldest | Votes 42 22 scanf("%e") reads the newline character from the ( ENTER | key. When you type let's say 15, you type a 1, a 5 and then press the | enrer | key. So there are now three characters in the input buffer. scanf("xa") reads the 1 and the 5, interpreting them as the number 15, but the newline character is stil in the input buffer. The scanf(“#e") will immediately read this newline character, and the program will then go on to the next sean¢("%a") , and wait for you to enter a number. The usual advice is to read entire lines of input with fgets , and interpret the content of each line in a separate step. A simpler solution to your immediate problem is to add a getcnar() after each scanf ("xa") Share Improve this answer Follow edited Aug 1214 at 1402 answered Mar 5 '12 at 6:08 iy Stefan van den Akker Thomas Padron- 5933 7 41 58 McCarthy 26k 8 49 74 3. The scanf("Xd"); getchar(); combination fails if the user accidentally enters a space (or other garbage) after the number. -iIkkachu Jun 11 '17 at 10:14 The basic problem is that scanf() leaves the newline after the number in the buffer, and then reads it with x on the next pass. Actually, this is a good demonstration of why | don't use scanf() ;| use a line reader ( fgets() , for example) and sscanf() . Itis easier to control. You can probably rescue it by using * xc” instead of “xc” for the format string. The blank ‘causes scanf() to skip white space (including newlines) before reading the character. hitpslstackoverfiow.comiquestions'9562218/¢-mulile-scan(s-wher--entr-in-a-alue-forone-scant-i-skips-the-second-s 28 ‘i912, 10-44 aM (C: Mutiple scans, whan entarina value fr one scarf skips the second scan- Slack Overtow But it will be easier in the long run to give up scanf() and fscanf() and use fgets() oF equivalent plus sscanf() . All else apart, error reporting is much easier when you have the whole string to work with, not the driblets left behind by scanf() after it fails. You should also, always, check that you get a value converted from scan() . Input fails — routinely and horribly. Don't let it wreck your program because you didn't check. Share Improve this answer Follow answered Mar 5°12 at 7.05 Jonathan Leffler 681k 128 834 1204 Try adding a space in the scanf. 8 scanf(" Xd", &var); Woo i there This will cause scanf() to discard all whitespace before matching an integer. Share Improve this answer Follow edited Sep 1"16 at 97.23 answered Jun 18 "15 at 21:14 @ Toby Speight Jon Z. 243k 47 58 89 1133 8 1 According to section 721.6298, seanf would normally discard all whitespace before matching an integer, anyway, rendering the whitespace in this format string pointless: "Input white-space characters (as specified by the isspace function) are skipped, unless the specification includes a {,c, or n specifier: ~ autistic Feb 21 '17 at 2:14 1. This won't fix the issue. X¢_already skips leading whitespace characters. - Spikatrix Feb 21 "17 at 2:43 Use the function 6 void seek to_next_line( void ) ‘ int while( (c= fgete( stdin )) I= EOF @& ¢ I= ‘\n" )5 > to clear out your input buffer. Share Improve this answer Follow edited Mar 25 '18 at 5:18. answered Mar 512 at 6:25 Mathieu K. & noMAD 23313 7AS4 17 53. 92 This is a good function. For readability, | would put the null statement (the single semicolon) on a separate line. ~ Thomas Padron-MecCarthy Mar 512 at 645. ° 1 also like this, but | think it'd be best nof to use the word “flush’, as this has an established definition within the standard which contradicts the use here. Perhaps seek_to_next_line would be amore hitpslstackoverfiow.comiquestions'9562218/¢-mulile-scan(s-wher--entr-in-a-alue-forone-scant-i-skips-the-second-s aia ‘W972, 10:41 AM C: Multiple scants, when | antrin a value for one scarf skips the secand scant - Slack Overlow descriptive name? ~ autistic Feb 21 ‘17 at 2:11 Making @Sebivor's proposed change. ~ Mathieu K. Mar 24 18 at 22:09 ‘Also putting this here since I can't add my own answer, and this is the best answer I see: scanf("%* [*\n]"); getchar(); . You can wrap this into a function, ifyou like. There's no benefit over this answer, both are technically fine, though the answer is easier to maintain, which is sort of a double-edged sword in this case; usually we trend towards writing more maintainable code but with idiots about the simpler version might be more likely to be modified to something incorrect... you know what I'm talking about... a certain optimisation. Choose the correct tool per job, right? autistic Mar 25 °18 at 12:38 The ‘\n" character is still left on the input stream after the first call to. scanf is completed, so the second call to scanf() reads it in. Use getchar() Share Improve this answer Follow edited Aug 1214 at 1404 answered Mar 5 “12 at 6:19 i Stefan van den Akker 8 Gargi Srinivas 5933 7 41 58 19 6 6 When you type the shape and ENTER, the shape is consumed by the first scant, but the ENTER is not! The second scanf expects a number so, the ENTER is skipped because is considered a 1 white space, and the scanf waits for a valid input (a number) that, again, is terminated by the ENTER. Well, the number is consumed, but the ENTER is not, so the first scanf inside the while uses it and your shape prompt is skipped... this process repeats. You have to add another %c in the scanfs to deal with the ENTER key. | hope this helps! Share Improve this answer Follow answered Mar ares You can also use scanf("XeX*e", &c); to read two characters and ignore the last one (in this case ‘\n’) 0 Share Improve this answer Follow answered Jul 115 at 946 Byeonggon Lee 186 10 hitpslstackoverfiow.comiquestions'9562218/¢-mulile-scan(s-wher--entr-in-a-alue-forone-scant-i-skips-the-second-s a

You might also like