程序自动终止,无需等待我的回复。为什么? [重复]

时间:2022-10-14 17:01:37

This question already has an answer here:

这个问题在这里已有答案:

I was making a program to enter numbers into a stack and the do-while loop was automatically finished without waiting for my response. Hence only one data was taken and displayed.

我正在编写一个程序将数字输入堆栈,并且do-while循环自动完成而不等待我的响应。因此,只拍摄并显示了一个数据。

#include <stdio.h>
#include <stdlib.h>

struct node
{
    int data;
    struct node *next;
};
typedef struct node NODE;

NODE *top = NULL;

void push(int x)
{
    NODE *p;

    p = (NODE*)malloc(sizeof(NODE));
    p->data = x;
    p->next = top;
    top = p;
}

void display(void)
{
    NODE *t;

    t = top;
    if(t == NULL)
    {
        printf("\nstack is empty");
    }
    else
    {
        while(t != NULL)
        {
            printf("%d ", t->data);
            t = t->next;
        }
    }
}

int main(void)
{
    int m;
    char ans;

    do
    {
        printf("\nEnter the no. to insert in stack: \n");
        scanf("%d", &m);
        push(m);

        printf("\nDo you want to enter more data???\n");
        scanf("%c", &ans);
    } while(ans == 'y' || ans == 'Y'); // here after entering a value for variable 'm', the program terminates displaying the stack with one element.

    display();
    return 0;
}

2 个解决方案

#1


4  

Please change

scanf("%c", &ans);

to

scanf(" %c", &ans);

Notice the added space, which consumes the newline which was left in the input buffer after the previous input.

注意添加的空间,它占用了在前一个输入之后留在输入缓冲区中的换行符。

Note that some format specifiers such as %d and %s automatically consume any leading whitespace, and leave in the buffer the next character which does not suit the format. In the case of your %d that was a newline.

请注意,某些格式说明符(例如%d和%s)会自动使用任何前导空格,并在缓冲区中留下不适合格式的下一个字符。如果您的%d是换行符。

The format %c however, collects the next character from the input buffer no matter what it is, and the leading space prevents that.

但是,格式%c从输入缓冲区收集下一个字符,无论它是什么,前导空格都会阻止它。

#2


1  

Besides the adding of the space in format string to consume the newline mentioned above, it's also a good practice to check scanf return value, as it might be failed to input an integer value and still push the old value of m onto the stack.

除了在格式字符串中添加空格以使用上面提到的换行符之外,检查scanf返回值也是一个好习惯,因为它可能无法输入整数值并仍然将m的旧值压入堆栈。

int main(void)
{
    int m;
    char ans;
    int ret;
    do
    {
        printf("\nEnter the no. to insert in stack: \n");
        ret = scanf("%d", &m);
        if (ret != 1) {
            printf("invalid input\n");
            continue;
        }
        push(m);

        printf("\nDo you want to enter more data???\n");
        ret = scanf(" %c", &ans);
        if (ret != 1) {
            printf("invalid input\n");
            continue;
        }
    } while(ans == 'y' || ans == 'Y');
}

#1


4  

Please change

scanf("%c", &ans);

to

scanf(" %c", &ans);

Notice the added space, which consumes the newline which was left in the input buffer after the previous input.

注意添加的空间,它占用了在前一个输入之后留在输入缓冲区中的换行符。

Note that some format specifiers such as %d and %s automatically consume any leading whitespace, and leave in the buffer the next character which does not suit the format. In the case of your %d that was a newline.

请注意,某些格式说明符(例如%d和%s)会自动使用任何前导空格,并在缓冲区中留下不适合格式的下一个字符。如果您的%d是换行符。

The format %c however, collects the next character from the input buffer no matter what it is, and the leading space prevents that.

但是,格式%c从输入缓冲区收集下一个字符,无论它是什么,前导空格都会阻止它。

#2


1  

Besides the adding of the space in format string to consume the newline mentioned above, it's also a good practice to check scanf return value, as it might be failed to input an integer value and still push the old value of m onto the stack.

除了在格式字符串中添加空格以使用上面提到的换行符之外,检查scanf返回值也是一个好习惯,因为它可能无法输入整数值并仍然将m的旧值压入堆栈。

int main(void)
{
    int m;
    char ans;
    int ret;
    do
    {
        printf("\nEnter the no. to insert in stack: \n");
        ret = scanf("%d", &m);
        if (ret != 1) {
            printf("invalid input\n");
            continue;
        }
        push(m);

        printf("\nDo you want to enter more data???\n");
        ret = scanf(" %c", &ans);
        if (ret != 1) {
            printf("invalid input\n");
            continue;
        }
    } while(ans == 'y' || ans == 'Y');
}