Reading newline from previous input when reading from keyboard with scanf()

前端 未结 6 1183
执念已碎
执念已碎 2020-11-30 11:15

This was supposed to be very simple, but I\'m having trouble to read successive inputs from the keyboard.

Here\'s the code:

#include 

        
6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-30 11:48

    First scanf read the entered string and left behind \n in the input buffer. Next call to scanf read that \n and store it to character.
    Try this

    scanf (" %c", &characte);   
         // ^A space before %c in scanf can skip any number of white space characters. 
    

    Program will not work for strings more than one character because scanf stops reading once find a white space character. You can use fgets instead

     fgets(string, 200, stdin);
    

提交回复
热议问题