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_Dynamic - Fatal编程技术网

C 如何动态获取用户输入并将其存储到整数数组中

C 如何动态获取用户输入并将其存储到整数数组中,c,dynamic,C,Dynamic,我有一个整数数组,比如inta[50]。现在,我想将用户在整数数组中输入的值存储为整数。 问题是我不知道用户要输入的元素数量,因此我无法遍历整个数组 因此,用户是否有任何方法可以动态输入值并将其存储在整数数组中并显示它。您可以使用realloc并在输入结束时进行特定的输入 int readinput(int *value) { int status; //function returns 0 on conversion -1 if exit keywoard entered

我有一个整数数组,比如
inta[50]。现在,我想将用户在整数数组中输入的值存储为整数。
问题是我不知道用户要输入的元素数量,因此我无法遍历整个数组


因此,用户是否有任何方法可以动态输入值并将其存储在整数数组中并显示它。

您可以使用realloc并在输入结束时进行特定的输入

int readinput(int *value)
{
    int status;
    //function returns 0 on conversion -1 if exit keywoard entered
    return status;
}


int value, *table = NULL,size = 0;
while (!readinput(&value))
{
    table = realloc(table, (size++ + 1) * sizeof(int));
    if (table == NULL)
    {
        break;
    }
    table[size] = value;
}
您可以在此处找到转换函数的示例:
在我的回答中,

为此,从用户处获取一个数组中元素数量的输入。然后malloc该内存并将输入存储在数组中。

以下解决方案使用with。while循环检查返回值,以便检测转换是否成功。如果输入的不是有效数字,则循环将中断。有效数字也以空格开头,但不以任何其他字符开头

每次用户输入一个数字时,内存将被分配并重新分配。请注意,对于重新分配没有错误检查,这应该使用临时指针来完成

此外,此代码将为每个号码重新分配。您还可以以更大的步骤重新分配,以避免在每个输入上重新分配。但这只有在有大量数据的情况下才重要。在这种情况下,速度的提高并不重要

在不再需要内存后,您必须使用它

用户可以键入:

1<Enter>
2<Enter>
3<Enter>
any characters<Enter>
作为输出

代码:

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

int main (int argc, char *argv[])
{
   size_t idx;
   int number;
   size_t count = 0;
   int* numberArr = malloc(sizeof(*numberArr));

   printf("Enter each number separated by <Enter>,\n"
          "to abort type any other character that isn't a number!\n");
   while (scanf("%d", &number) == 1)
   {
      numberArr = realloc(numberArr, (count + 1) * sizeof(*numberArr));
      numberArr[count] = number;
      count++;
   }

   printf("\nNumbers entered:\n");
   for (idx = 0; idx < count; idx++)
   {
      printf("%d ", numberArr[idx]);
   }
   printf("\n");

   free(numberArr);

   return 0;
}
#包括
#包括
int main(int argc,char*argv[])
{
尺寸(idx);;
整数;
大小\u t计数=0;
int*numberrarr=malloc(sizeof(*numberrarr));
printf(“输入每个数字,\n”
“要中止,请键入任何其他非数字字符!\n”);
while(scanf(“%d”,&number)==1)
{
numberrarr=realloc(numberrarr,(count+1)*sizeof(*numberrarr));
numberrarr[计数]=数字;
计数++;
}
printf(“\n输入的编号:\n”);
对于(idx=0;idx
此代码应能正常工作,您可以将
缓冲区大小
更改为您想要的任何大小,在这些步骤之后,数组将
realloc
更改为
arr大小
+
缓冲区大小

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

#define BUFFER_SIZE 50

int main(void)
{
    int * userInputBuffer = malloc(sizeof(int) * BUFFER_SIZE);
    int userInput;
    int counter = 0;
    int reallocCounter = 1;

    while ((scanf(" %d", &userInput)) == 1)
    {
        if ((counter % BUFFER_SIZE) == 0)
        {
            userInputBuffer = realloc(userInputBuffer, (reallocCounter++ + 1) * BUFFER_SIZE * sizeof(int));
        }
        userInputBuffer[counter++] = userInput;
    }

    for (int i = 0; i < counter; i++)
    {
        printf("User input #%d: %d\n", i + 1, userInputBuffer[i]);
    }

    free(userInputBuffer);
    return 0;
}
#包括
#包括
#定义缓冲区大小为50
内部主(空)
{
int*userInputBuffer=malloc(sizeof(int)*缓冲区大小);
int用户输入;
int计数器=0;
int reallocCounter=1;
while((scanf(“%d”,&userInput))==1)
{
if((计数器%BUFFER\u SIZE)==0)
{
userInputBuffer=realloc(userInputBuffer,(reallocCounter+++1)*缓冲区大小*大小(int));
}
userInputBuffer[counter++]=userInput;
}
对于(int i=0;i
您可以在每个用户输入后为数组重新分配内存。您需要在堆上动态分配内存。每次输入后,您都需要使用realloc()函数来扩展“数组”。用户应该如何“告诉”程序输入了最后一个数字?答案取决于此。如果用户有4个带有数字的页面,你会强迫他在输入之前把它们全部数完吗:)这更像是一条评论,而不是一个答案。它被标记为
C
,但你使用
#include
,它是
C++
。如果你测试你的程序,你应该使用一个简单的C编译器,它会失败。嗨,Mistique,如果这个或任何答案已经解决了你的问题,请考虑点击检查标记。这向更广泛的社区表明,你已经找到了一个解决方案,并给回答者和你自己带来了一些声誉。没有义务这样做。
#include <stdio.h>
#include <stdlib.h>

#define BUFFER_SIZE 50

int main(void)
{
    int * userInputBuffer = malloc(sizeof(int) * BUFFER_SIZE);
    int userInput;
    int counter = 0;
    int reallocCounter = 1;

    while ((scanf(" %d", &userInput)) == 1)
    {
        if ((counter % BUFFER_SIZE) == 0)
        {
            userInputBuffer = realloc(userInputBuffer, (reallocCounter++ + 1) * BUFFER_SIZE * sizeof(int));
        }
        userInputBuffer[counter++] = userInput;
    }

    for (int i = 0; i < counter; i++)
    {
        printf("User input #%d: %d\n", i + 1, userInputBuffer[i]);
    }

    free(userInputBuffer);
    return 0;
}