【问题标题】:Why does the program exit, without waiting for the user input?为什么程序不等待用户输入就退出了?
【发布时间】:2020-07-20 11:49:25
【问题描述】:

这个 C 程序应该根据用户输入的元素数量分配内存,然后添加它们并打印结果。它再次提示用户是否要添加更多数字。但是在输入 Y/N 时,控制台关闭并且程序意外结束。如何解决?

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

int main()
{
    int n,i;
    int *ptr,*old_ptr;
    int sum = 0;
    char a;

    printf("Enter the number of elements to be added: ");
    scanf("%d",&n);

    ptr = calloc(n,sizeof(int));
    ptr = old_ptr;

    printf("Enter the elements:\n");

    for(i = 0;i < n;i++){
        scanf("%d",ptr);
        sum = sum + *ptr;
        ptr++;
    }
    printf("The sum total of the numbers is: %d\n\n\n",sum);

    printf("Do you want to enter more numbers ?\nPress Y/N: ");
    scanf("%c",&a);
    if(a == 'Y'){
        printf("\n\nYou have entered: %c\n\n",a);
        ptr = realloc(old_ptr,sizeof(int)*n);

        printf("Enter the elements:\n");

    for(i = 0;i < n;i++){
        scanf("%d",&ptr);
        sum = sum + *ptr;
        ptr++;
    }
    printf("The total of the numbers is: %d\n\n\n",sum);
    }
    if(a == 'N'){
        printf("Program finished!");
    }
    return 0;
}

【问题讨论】:

  • 这是什么 ptr = calloc(n,sizeof(int)); ptr = old_ptr;什么意思?!
  • 也许你的意思是old_ptr = ptr;?但是根本不需要分配数组。您可以每次都读入相同的int 变量并将其添加到sum,因为您实际上并没有真正使用该数组。
  • 我相信(不是专家)您需要使用 scanf 格式字符串 " %s"(注意空格),这样它就会忽略空格和换行符。
  • @VladfromMoscow 感谢您的回复。我为指针分配了一些内存,然后在提示后的第二次尝试和从用户那里获得输入后,我重新分配了内存。
  • @kaylum 感谢您的回复。不,这也不起作用。

标签: c memory-management crash scanf


【解决方案1】:

请查看修改后的代码。

scanf("%c",&a);

问题:scanf 从输入缓冲区读取\n (ASCII:10),并且不再等待用户输入。 a 中存储的值将是 10 (\n)。由于a 不等于“Y”或“N”,程序退出。

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

int main()
{
    int n,i;
    int *ptr,*old_ptr;
    int sum = 0;
    char a;

    printf("Enter the number of elements to be added: ");
    scanf("%d",&n);

    ptr = calloc(n,sizeof(int));
    old_ptr=ptr;

    printf("Enter the elements:\n");

    for(i = 0;i < n;i++){
        scanf("%d",ptr);
        sum = sum + *ptr;
        ptr++;
    }
    printf("The sum total of the numbers is: %d\n",sum);

    printf("Do you want to enter more numbers ?Press Y/N: \n");
    getchar();
    scanf("%c",&a);
    if(a == 'Y'){
        printf("You have entered: %c\n\n",a);
        ptr = realloc(old_ptr,sizeof(int)*n);

        printf("Enter the elements:\n");

    for(i = 0;i < n;i++){
        scanf("%d",ptr);
        sum = sum + *ptr;
        ptr++;
    }
    printf("The total of the numbers is: %d\n\n\n",sum);
    }
    if(a == 'N'){
        printf("Program finished!");
    }
    return 0;
}

需要的更改是:

  1. ptr = old_ptr 更改为old_ptr=ptr。 原因:赋值运算符的结合性是从右到左的。

  2. scanf("%d",&amp;ptr) 更改为scanf("%d",ptr) 原因:ptr 持有所需的地址。不需要&amp;

  3. getchar函数放入读取额外的\n

  4. 删除printf 语句中多余的\n

经过这些修改后,它起作用了。请看图。

【讨论】:

  • 感谢您的回复。是的,您的编辑在很大程度上解决了这个问题。但是还有两件事需要注意:1)[已解决]我必须将代码的第二部分放在一个do-while循环中才能继续添加。 2)[未解决]如何根据用户想要添加的元素数量增加每次迭代的内存?目前它被固定在输入元素数量的第一个条目上。例如,第一次他们想添加 4 个,第二次他们想添加 5 个或 6 个。
  • 1) 您需要在另一个变量中读入 (scanf) 新的元素数量。在这里,在上面的代码中,我们只是使用了之前的n。 2) 不要使用更多的\n,因为scanf 可能会读取\n (ASCII:10)。 3)然后使用 realloc() 。 4)请参阅此以正确使用realloc() --> geeksforgeeks.org/g-fact-66
  • 是的,只是第二个 for 循环中的输入错误;正在使用 n 而不是新变量。问题100%解决。 \../
猜你喜欢
  • 1970-01-01
  • 2015-02-24
  • 2015-05-15
  • 2021-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多