News:

This week IPhone 15 Pro winner is karn
You can be too a winner! Become the top poster of the week and win valuable prizes.  More details are You are not allowed to view links. Register or Login 

Main Menu

C++ Tips : IO - How to politely eat a newline (with pinkey extended, of course)

Started by ben2ong2, October 06, 2006, 11:09:44 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

ben2ong2

PROBLEM: You are not allowed to view links. Register or Login (Douglas K. Bell)

   In the following code,  "gets" never executes!  Why ???
   If the "scanf" is removed, "gets" runs fine.  Why ????
   ( Argh... )   

   main()
   {
      long serial_number;
      char buf[80];

      scanf( "%ld", &serial_number );

      gets( buf );      <<<<<<<<<  NEVER EXECUTES !!!  WHY ?
   }


RESPONSE: You are not allowed to view links. Register or Login (Jeffrey C. Ollie), 7 Dec 93

gets does execute.  The scanf will leave the next input as the first character
after the serial number.  This is probably '\n'.  When gets executes, it sees
a '\n' as it's first input, takes this to be the end of a line, and exits.

Add "%*[^\n]%*c" to the end of your format string.  This will eat everything up
intil the end of a line, and then eat the newline.

You are not allowed to view links. Register or Login
You are not allowed to view links. Register or Login