错过了 scanf 并且功能在没有它的情况下继续。如果我添加 space 仍然不起作用

Missed scanf and function goes on without it. If I add a space still doesn't work

#include <stdio.h>


struct mychar {
    char value;
    struct mychar *nextPtr;
};

typedef struct mychar Mychar;


void instructions();
void append(Mychar **, char );
void printlist(Mychar *);


int main(){
    instructions();

    Mychar *startPtr = NULL;

    unsigned int choice;
    char newchar;
    do {
        scanf("%d",&choice);
        switch (choice) {
            case 1:
                printf("\nWrite the character you want to add.");
                printf("\n> ");
                scanf(" %c", &newchar);
                append(&startPtr, newchar);
                printlist(startPtr);
                break;
            case 2:
                break;
            default:
                printf("\nError, try again.\n");
                //main();
                instructions();
                break;
        }
    } while (choice!=3);
    printf("\n\nEnd of run.\n");
}


void instructions(){
    printf("\nSelect operation. 1 to add, 2 to remove, 3 to exit.");
    printf("\n> ");
}


void append(Mychar **sPtr, char newvalue){
    Mychar *newlinkPtr = calloc (1, sizeof(Mychar));
    newlinkPtr->value = newvalue;
    newlinkPtr->nextPtr = NULL;

    Mychar *previousPtr = NULL;
    Mychar *currentPtr = *sPtr;

    while(currentPtr!=NULL && newvalue > currentPtr->value){
        previousPtr = currentPtr;
        currentPtr = currentPtr->nextPtr;
    }

    if (previousPtr){
        previousPtr->nextPtr = newlinkPtr;
        newlinkPtr->nextPtr = currentPtr;
    } else {
        *sPtr = newlinkPtr;
    }

}


void printlist(Mychar *currentPtr){
    printf("\n\nCurrent list:\n");
    while (currentPtr!=NULL){
        printf("%c", currentPtr->value);
        currentPtr = currentPtr->nextPtr;
    }
}

为什么我会有这种行为?如果我 运行 程序,在我输入 1 后,它会打印 "current list" 并保持 scanf 输入打开,所以我只能在 "current list" 打印后输入值。另外,"current list" 应该只在我用 scanf 输入字符后调用,因为函数 printlist 在 scanf 之后......但实际上是这样的:

Select operation. 1 to add, 2 to remove, 3 to exit.
> 1

Write the character you want to add.
> a


Current list:
ab

Write the character you want to add.
> 

Current list:
abc

Write the character you want to add.
> 

Current list:
abcd

Write the character you want to add.
> 

Current list:
abcd

从中汲取的教训是始终检查 scanf 0 return,至少,EOF 也建议检查,并采取相应措施,至于你的代码的事件顺序,它并不完全存在,通过一些调整你可以得到一个很好的,错误的输入证明,I/O sequence:

void clear_stdin() { //stdin buffer clearing function 
    int c;
    while ((c = getchar()) != '\n' && c != EOF){}
}
do {
    instructions(); //move inside the loop, user will be prompted in each cycle
    while (scanf("%d", &choice) == 0) {
        printf("\nError, try again.\n");
        instructions();
        clear_stdin(); // if input fails clear the buffer
    }
    clear_stdin(); // clear the buffer for 1hjh type input
    switch (choice) {
    case 1:

        printf("\nWrite the character you want to add.");
        printf("\n> ");
        while (scanf(" %c", &newchar) == 0) { //this can be a pattern
            clear_stdin();                    //see @ismick comment
        }                                     //
        clear_stdin();                        //
        append(&startPtr, newchar);
        printlist(startPtr);
        break;
    case 2:
        break;
    case 3:
        printf("\n\nEnd of run.\n"); //if you dont have a case default will catch 3
        break;
    default:
        printf("\nError, try again.\n");
        break;
    }
} while (choice != 3);