c language input single characters are four ways to avoid carriage returns

  1. Use '\ n' in scanf () shield the carriage return.

    Scanf ( " % D \ n- " , & n-); // use '\ n' Enter filter 
    Scanf ( " % C " , & C);

    or

    scanf("%d",&x);
    scanf("\n%c",&c);

     

  2. In scanf () format string front add a space, a carriage return character mask

    Scanf ( " % D " , & n-); 
    Scanf ( " % c " , & c); // % c front spaces, carriage returns filtered

     

  3. Before receiving the characters, using getchar () to read a carriage return symbol

    Scanf ( " % D " , & n-); 
    getchar ();   // dedicated to reading the last symbol entered the transport 
    Scanf ( " % C " , & C);

     

  4. Before receiving characters using fflush () Clear the contents of the input stream buffer

    Scanf ( " % D " , & n-) 
    fflush (stdin);   // flush character input stream buffer, attention must be introduced #include <stdlib.h> header 
    Scanf ( " % C " , & C);

     

Guess you like

Origin www.cnblogs.com/Theo-sblogs/p/11461507.html