How to User Input Array in Function in C++

Mohd Mohtashim Nawaz Feb 02, 2024
  1. Take User Input Array in a Function by Declaring a Global Array
  2. Take User Input Array in a Function by Declaring Array in the Function
  3. Take User Input Array in a Function by Declaring the Array in the Main Function
  4. Take User Input Array in a Function by Using Dynamic Memory Allocation
  5. Conclusion
How to User Input Array in Function in C++

Arrays are fundamental data structures in programming that allow users to store and manage collections of data. Often, we need to gather data from users and store it in an array within a C++ program.

This topic involves taking user-provided data and storing it in an array defined within a function. This article will discuss how to create functions that accept user input to populate arrays in C++.

Take User Input Array in a Function by Declaring a Global Array

To obtain the user input into the array, you must have access to the array. One of the easiest ways is to declare a global array.

You can declare a global array in C++ by simply declaring it outside all the functions in the global scope. You must also declare a global variable that stores the size of the array.

After declaring the array, you must loop through all the array elements using any loop and store the user input into the current array index.

Source Code:

#include <iostream>

using namespace std;

int size = 5;
int arr[5];

void userInput() {
  cout << "Enter array elements" << endl;
  for (int i = 0; i < size; i++) {
    cin >> arr[i];
  }
}

void print() {
  cout << "Array elements are:" << endl;
  for (int i = 0; i < size; i++) cout << arr[i] << " ";
  cout << endl;
}

int main() {
  userInput();
  print();
  return 0;
}

In this approach, the array size is specified by an integer (arr[5]), not a variable like size = 5.

The userInput() function is responsible for taking user input and filling the arr array. It starts by displaying the message "Enter array elements" on the console.

Then, it uses a for loop to iterate from 0 to size - 1 (from 0 to 4 in this case) and takes user input for each element, storing them in the arr array.

The print() function displays the elements of the arr array by printing the message "Array elements are:" on the console. Then, it uses a for loop to iterate through the arr array and print each element followed by a space.

Lastly, the main() function is the entry point of the program. It calls the userInput function to get user input and fill the arr array.

It then calls the print function to display the elements of the arr array.

Output:

Enter array elements
1 8 5 3 7
Array elements are:
1 8 5 3 7

The output shows the user input values (1, 8, 5, 3, 7) into the array, and then the program prints the values.

Take User Input Array in a Function by Declaring Array in the Function

Another method to gather user input into an array is by declaring the array directly within the function where you need it. Here, the array becomes a local variable within the function.

You can loop through the array, collect user input, and store it in the array.

Source Code:

#include <iostream>

using namespace std;

void userInput() {
  int size = 5;
  int arr[size];

  cout << "Enter array elements" << endl;
  for (int i = 0; i < size; i++) {
    cin >> arr[i];
  }

  cout << "Array elements are:" << endl;
  for (int i = 0; i < size; i++) cout << arr[i] << " ";
  cout << endl;
}

int main() {
  userInput();
  return 0;
}

The userInput() is a function defined to get user input and display it.

The function declares an integer variable size and sets it to 5 (it represents the size of the array). Then, we declare an integer array arr[size] (which is 5 in this case).

The next part of the code asks the user to enter the elements of the array. Then enters a loop that runs from i = 0 to i = size - 1 (0 to 4 in this case).

Inside the loop, it uses cin (input stream) to read integers from the user and stores them in the array arr at the index i (arr[i]). After getting all the input elements, the code displays the message "Array elements are:".

Then enters another loop that iterates through the array and prints each element separated by a space. Finally, it prints an empty line to format the output.

The main() function calls the userInput() function to execute the code within that function.

Output:

Enter array elements
8 4 7 2 2
Array elements are:
8 4 7 2 2

Same as the above code, it shows the user input values (8, 4, 7, 2, 2) into the array, and then the program prints the array elements.

Take User Input Array in a Function by Declaring the Array in the Main Function

If you declare the array in the main function, you can not directly access it inside of the function from where you are taking the user input. Therefore, you must pass the array as a parameter to the function and the array’s size.

Arrays are passed by reference by default in C++, so changes made in the function will reflect in the main function’s array.

This code is similar to the previous one. It still takes user input to populate an array and then displays the elements of that array, but it is structured a bit differently and uses separate functions for input and output.

Source Code:

#include <iostream>
using namespace std;

void userInput(int arr[], int size) {
  cout << "Enter array elements" << endl;
  for (int i = 0; i < size; i++) {
    cin >> arr[i];
  }
}

void print(int arr[], int size) {
  cout << "Array elements are:" << endl;
  for (int i = 0; i < size; i++) cout << arr[i] << " ";
  cout << endl;
}

int main() {
  int size = 5;
  int arr[size];

  userInput(arr, size);
  print(arr, size);
  return 0;
}

The userInput() function takes two arguments: an integer array arr[] and an integer size. It is responsible for getting user input and populating the array.

The next part of the code asks the user to enter the elements of the array. Then, enters a loop that runs from i = 0 to i = size - 1.

Inside the loop, it uses cin (input stream) to read integers from the user and stores them in the array arr[i] at the index i.

The print() function is another function that takes two arguments: an integer array arr[] and an integer size. It is responsible for displaying the elements of the array.

It enters a loop that iterates through the array and prints each element separated by a space. Then, it prints an empty line to format the output.

The main() function declares an integer size and sets it to 5, indicating that we want to create an array of size 5 and an integer array arr[size] (which is 5 in this case). The main() function calls the userInput() function, passing the array arr and its size as arguments to populate it with user input.

Output:

Enter array elements
4 5 9 7 2
Array elements are:
4 5 9 7 2

It shows the user input values (4, 5, 9, 7, 2) into the array, and then the program prints the array elements.

Take User Input Array in a Function by Using Dynamic Memory Allocation

In this method, you can allocate memory for the array dynamically using the new keyword. This approach allows you to create an array with a variable size based on user input.

The code example below demonstrates dynamic memory allocation for an array. It allows the user to specify the size of the array at runtime, populate it with values, and then display the elements.

Source Code:

#include <iostream>
using namespace std;

void userInput() {
  int size;
  cout << "Enter the size of the array: ";
  cin >> size;

  int* arr = new int[size];

  cout << "Enter array elements:" << endl;
  for (int i = 0; i < size; i++) {
    cin >> arr[i];
  }

  cout << "Array elements are:" << endl;
  for (int i = 0; i < size; i++) cout << arr[i] << " ";
  cout << endl;
  delete[] arr;
}

int main() {
  userInput();
  return 0;
}

The userInput is a function defined to get user input for the array size and elements. It declares an integer variable size to store the size of the array and then prompts the user to enter the size of the array and reads the input using cin.

After getting the size from the user, the code dynamically allocates an integer array arr of the size specified by the user using the new keyword. This means that the memory for the array is allocated on the heap, and it exists beyond the scope of this function.

The next line of code prompts the user to enter the elements of the array. It enters a loop that runs from i = 0 to i = size - 1, and inside the loop, it uses cin to read integers from the user and stores them in the dynamically allocated array arr[i].

After getting all the input elements, it will display the elements of the array. It enters another loop that iterates through the dynamically allocated array, prints each element separated by a space, and prints an empty line to format the output.

In order to prevent memory leaks, the code uses the delete[] operator to release the dynamically allocated memory for the array arr. This step is crucial when using dynamic memory allocation.

The main() function calls the userInput() function to execute the code within that function.

Output:

Enter the size of the array: 4
Enter array elements:
8 4 7 2
Array elements are:
8 4 7 2

It shows the size of the array. The user inputs values (8, 4, 7, 2) into the array, and then the program prints the array elements.

Conclusion

In summary, this article presented four methods for obtaining user input into an array within a C++ function. While all methods are functional, it is advisable to define your array within the main function (as shown in the last method).

This approach allows for passing the array to other functions without potential issues related to global variables, offering better control over the scope and lifetime within your program.

Related Article - C++ Array