2

I have an integer array like int a[50];. Now I want to store values entered by the user in the integer array as integers.
The problem is that I don't know the number of elements the user is going to input and hence I am unable to traverse the entire array.

So is there any method for the user to input the values dynamically and store it in the integer array and display it.

3
  • 1
    you can realloc memmory for array after each user input Commented Aug 11, 2017 at 8:12
  • you need to allocate memory dynamically on the heap. You will need to use the realloc() function to expand your "array" after every input. Commented Aug 11, 2017 at 8:13
  • 2
    How should the user "tell" the program that the last number has been entered? The answer dépends on this. Commented Aug 11, 2017 at 8:58

4 Answers 4

0

For this take a input from user for number of element to have in an array. Then malloc that memory and store the inputs in the array.

Sign up to request clarification or add additional context in comments.

2 Comments

and if user have 4 pages with numbers - will you force him to count them all before entering :)
This is more like a comment rather an answer.
0

You can use realloc and make a specific input as end of the input

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;
}

example converting function you can find here: How to get Integer and float input without `scanf()` in c? in my answer

Comments

0

This code should work well, you can change BUFFER_SIZE to whatever you want, after these steps array will realloc to arr size + BUFFER_SIZE.

#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;
}

1 Comment

It's tagged as C but you use #include <iostream> which is C++. If you test your programs you should use a plain C compiler which will fail on that.
0

The following solution uses the scanf() function with %d as format specifier. The while loop checks the return value so that it can detect if the conversion was successful. If anything other than a valid number is inputted the loop will break. A valid number is also beginning with space but not with any other characters.

The memory is allocated with malloc() and will be reallocated with realloc() each time the user entered a number. Note that there is no error checking about the reallocation this should be done with a temporary pointer like here.

Further this code will reallocate for every single number. You could also reallocate in bigger steps to avoid reallocation on every input. But this only matters if there is much data. In this case the speed improvement wouldn't matter.

After the memory is no longer needed you have to use free().

The user can type:

1<Enter>
2<Enter>
3<Enter>
any characters<Enter>

and will get:

Numbers entered:
1 2 3

as output.

Code:

#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;
}

1 Comment

Hi @Mistique if this or any answer has solved your question please consider accepting it by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.