Why 2nd scanf doesn't work in my program?

前端 未结 3 776
孤街浪徒
孤街浪徒 2020-12-11 05:09

scanf(\"%d %c\",&size,&chara); works but separate scanf for character input does not work. I show these inside the code. Why is that?

void squareCust         


        
3条回答
  •  有刺的猬
    2020-12-11 05:56

    Scanf did not consume the \n character that stayed in the buffer from the first scanf call.

    So the second scanf call did.

    You have to clear the stdin before reading again or just get rid of the newline.

    The second call should be

    scanf(" %c",&chara);
           ^ this space this will read whitespace charaters( what newline also is) until it finds a single char
    

提交回复
热议问题