Problems with character input using scanf()

后端 未结 4 703
暖寄归人
暖寄归人 2020-11-27 22:24

I\'m trying to input a character into a linked list, where the character can be \'A\',\'a\',\'G\',\'g\',\'T\',\'t\',\'C\' or \'c\'.

I\'m not yet familiar with C and

4条回答
  •  自闭症患者
    2020-11-27 22:35

    You don't handle the newline. The %c specifier doesn't skip blanks. Try:

    scanf(" %c", &newChar);
        /* ^ <-- Makes `scanf` eat the newline. */
    

    Or maybe add an explicit test.

    scanf(...);
    if (newChar == '\n')
        continue;
    

提交回复
热议问题