Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C++ Data Types
  • C++ Input/Output
  • C++ Arrays
  • C++ Pointers
  • C++ OOPs
  • C++ STL
  • C++ Interview Questions
  • C++ Programs
  • C++ Cheatsheet
  • C++ MCQ
  • C++ Projects
  • C++ Exception Handling
  • C++ Memory Management
Open In App
Next Article:
Pass Array to Functions in C++
Next article icon

How to Take Input in Array in C++?

Last Updated : 05 Oct, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Arrays in C++ are derived data types that can contain multiple elements of the same data type. They are generally used when we want to store multiple elements of a particular data type under the same name.

We can access different array elements using their index as they are stored sequentially in the memory. We can also assign some value to these elements using their index and to assign the user-provided input value, we have to use cin or scanf().

But instead of accessing a single element to take input in an array, we use a loop to iterate over the array elements and take input from the user for each element.

C++ Program to Take User Input in an Array

The following C++ program uses a for loop and cin to take input from the user and insert it in the array.

C++
// C++ program to demonstrate how to take input in an array #include <iostream> using namespace std;  // driver code int main() {     // defining array size     int size = 5;     // defining array of size "size"     int numbers[size];      // using loop to move to every array element and then     // using either cin to insert the value given by the     // user to the array element     for (int i = 0; i < size; i++) {         cout << "Enter a number: ";         cin >> numbers[i];     }      // Print the array elements     cout << "The array elements are: ";     for (int i = 0; i < size; i++) {         cout << numbers[i] << " ";     }      return 0; } 


Output

Enter a number: 1   Enter a number: 1 1  Enter a number: 5   Enter a number: 8   Enter a number: 7  Array elements are: 1 11 5 8 7

C++ Program to Take User Input in an Array using scanf()

We can also use scanf() function to take input but then we have to specify the type of data and pass the address of the array element using & operator.

C++
// C++ program to demostrate how to take input to array // using scanf() #include <cstdio> #include <iostream>  using namespace std;  // driver code int main() {     // creating an array of size "size"     int size = 5;     int array[size];      // going to each element and assigning the value entered     // by the user     for (int i = 0; i < size; i++) {         cout << "Enter the element: ";         scanf("%d", &arr[i]);     }      // printing the array     cout << "The Array elements are: ";     for (int i = 0; i < size; i++) {         cout << arr[i] << ' ';     }      return 0; } 


Output

Enter a number: 6
Enter a number: 1
Enter a number: 22
Enter a number: 99
Enter a number: 8
Array elements are: 6 1 22 99 8



Next Article
Pass Array to Functions in C++
author
lostboi
Improve
Article Tags :
  • C++
  • cpp-array
  • C++ Array Programs
Practice Tags :
  • CPP

Similar Reads

  • Pointer to an Array in C++
    Pointers in C++ are variables that store the address of another variable while arrays are the data structure that stores the data in contiguous memory locations. In C++, we can manipulate arrays by using pointers to them. These kinds of pointers that point to the arrays are called array pointers or
    6 min read
  • Array Type Manipulation in C++
    This article demonstrates some of the inbuilt functions that can be used to query and manipulate array types, even a multidimensional array. These functions can be useful in cases we need information or manipulate array we initiated with different dimensions. These functions are defined in header fi
    5 min read
  • Pass Array to Functions in C++
    In C++, a collection of elements stored in contiguous memory locations and having the same data type is called an array. Passing arrays to functions is done to perform various operations on array elements without messing up with the main code. In C++, an array can be passed in a function using a poi
    5 min read
  • How to sort an Array using STL in C++?
    Given an array arr[], sort this array using STL in C++. Example: Input: arr[] = {1, 45, 54, 71, 76, 12} Output: {1, 12, 45, 54, 71, 76} Input: arr[] = {1, 7, 5, 4, 6, 12} Output: {1, 4, 5, 6, 7, 12} Approach: Sorting can be done with the help of sort() function provided in STL. Syntax: sort(arr, arr
    1 min read
  • How to Read and Print an Integer value in C++
    The given task is to take an integer as input from the user and print that integer in C++ language. In this article, we will learn how to read and print an integer value. In the below program, the syntax and procedures to take the integer as input from the user is shown in C++ language. Standard Inp
    2 min read
  • Array of Vectors in C++ STL
    Prerequisite: Arrays in C++, Vector in C++ STL An array is a collection of items stored at contiguous memory locations. It is to store multiple items of the same type together. This makes it easier to get access to the elements stored in it by the position of each element. Vectors are known as dynam
    3 min read
  • Pointers vs Array in C++
    Arrays and pointers are two derived data types in C++ that have a lot in common. In some cases, we can even use pointers in place of arrays. But even though they are so closely related, they are still different entities. In this article, we will study how the arrays and pointers are different from e
    3 min read
  • array::size() in C++ STL
    The array::size() method is used to find the number of elements in the array container. It is the member method std::array class defined inside <array> header file. In this article, we will learn about the array::size() method in C++. Example: [GFGTABS] C++ // C++ Program to illustrate the use
    2 min read
  • How to use getline() in C++ when there are blank lines in input?
    In C++, if we need to read a few sentences from a stream, the generally preferred way is to use the getline() function as it can read string streams till it encounters a newline or sees a delimiter provided by the user. Also, it uses <string.h> header file to be fully functional. Here is a sam
    2 min read
  • One Dimensional Arrays in C++
    One-dimensional arrays are like a row of boxes where you can store things where each box can hold one item, such as a number or a word. For example, in an array of numbers, the first box might hold 5, the second 10, and so on. You can easily find or change what's in each box by referring to its posi
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences