Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 程序自动终止,无需等待我的响应。为什么?_C_Stack_Do While - Fatal编程技术网

C 程序自动终止,无需等待我的响应。为什么?

C 程序自动终止,无需等待我的响应。为什么?,c,stack,do-while,C,Stack,Do While,我正在制作一个程序,将数字输入堆栈,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 =

我正在制作一个程序,将数字输入堆栈,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;
}
#包括
#包括
结构节点
{
int数据;
结构节点*下一步;
};
typedef结构节点;
NODE*top=NULL;
无效推送(整数x)
{
节点*p;
p=(NODE*)malloc(sizeof(NODE));
p->data=x;
p->next=顶部;
top=p;
}
作废显示(作废)
{
节点*t;
t=顶部;
如果(t==NULL)
{
printf(“\n堆栈为空”);
}
其他的
{
while(t!=NULL)
{
printf(“%d”,t->数据);
t=t->next;
}
}
}
内部主(空)
{
int m;
查尔安斯;
做
{
printf(“\n输入要插入堆栈的编号:\n”);
scanf(“%d”、&m);
推力(m);
printf(“\n您想输入更多数据吗?\n”);
scanf(“%c”和“&ans”);
}(ans=='y'| | ans=='y');//在这里输入变量'm'的值后,程序终止显示带有一个元素的堆栈。
显示();
返回0;
}
请更改

scanf("%c", &ans);

请注意添加的空间,它消耗了上次输入后留在输入缓冲区中的
换行符

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

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

请更改

scanf("%c", &ans);

请注意添加的空间,它消耗了上次输入后留在输入缓冲区中的
换行符

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


然而,格式
%c
从输入缓冲区收集下一个字符,不管它是什么,前导空格阻止了这一点。

除了在格式字符串中添加空格以使用上面提到的换行符外,检查
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');
}

除了在格式字符串中添加空格以使用上面提到的换行符外,检查
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');
}

scanf(“%c”和&ans)-->
scanf(“%c”和&ans)
注意添加的空间,它消耗了
换行符
,该换行符在
scanf(“%d”和&m)之后留在输入缓冲区中
每当我一个接一个地使用scanf
scanf时,我是否应该使用此方法?请查看我的答案。您键入了什么?我打赌大概是123。因此%d读取123,然后%c读取,并且不是y或y。
scanf(“%c”,&ans)-->
scanf(“%c”和&ans)
注意添加的空间,它消耗了
换行符
,该换行符在
scanf(“%d”和&m)之后留在输入缓冲区中
每当我一个接一个地使用scanf
scanf时,我是否应该使用此方法?请查看我的答案。您键入了什么?我打赌大概是123。所以%d读取123,然后%c读取,并且不是y或y。