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

纵然是瞬间 提交于 2020-06-01 05:09:02

问题


#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;
    }
}

Why do I have this behaviour? If I run the program, after I enter 1, it prints the "current list" and leave the scanf input opened, so I can enter the value only after "current list" printed. Also, "current list" should be called only after I enter the character with scanf, since the function printlist is AFTER the scanf... but actually this is what happens:

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

回答1:


The lesson to take form this is to always check scanf for 0 return, at the very least, EOF check is also advised, and act accordingly, as for the order of events of your code, it's not quite there, with some tweaks you can have a nice, bad input proof, 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);


来源:https://stackoverflow.com/questions/62072901/missed-scanf-and-function-goes-on-without-it-if-i-add-a-space-still-doesnt-wor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!