Пропущенный scanf, и функция продолжает работать без него. Если я добавлю пробел, все равно не получится - PullRequest
0 голосов
/ 28 мая 2020
#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, она распечатает «текущий список» и оставит вход scanf открытым, поэтому я могу ввести значение только после того, как напечатан «текущий список». Кроме того, "текущий список" должен вызываться только после того, как я ввожу символ с помощью 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

1 Ответ

1 голос
/ 29 мая 2020

Урок, который следует принять из этого, состоит в том, чтобы всегда проверять scanf для 0 возврата, по крайней мере, EOF проверка также рекомендуется, и действовать соответственно, что касается порядка событий вашего кода, это не совсем там, с некоторыми настройками вы можете получить хорошее, плохое подтверждение ввода, последовательность ввода-вывода:

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);
...