Read user input into Array in C++
On this page (6sections)
Syntax
any_loop{
// Based on Array Size
}
//12 is Array Size
for (i = 0; i < 12; i++) {
// Reading User Input sales value Based on index
cin >> sales[i]; //i is index
}
Example Program
/* Read User Input into Array example program In C++ Programming Language
Array Example In C++*/
// Header Files
#include <iostream>
#include<conio.h>
using namespace std;
int main() {
// declaring sales array in C
int sales[12];
int i; // Variable for access sales array index
cout << "Read User Input into Array In C++ Example Program\n";
for (i = 0; i < 12; i++) {
// Reading User Input sales value Based on index
cout << "Enter Value for Position " << i << " : ";
cin >> sales[i];
}
cout << "\n User Input Values\n";
for (i = 0; i < 12; i++) {
// Accessing sales value using for loop
cout << "Position : " << i << " , Sales Value : " << sales[i] << " \n";
}
getch();
return 0;
}
Sample Output
Read User Input into Array In C++ Example Program
Enter Value for Position 0 : 900
Enter Value for Position 1 : 200
Enter Value for Position 2 : 800
Enter Value for Position 3 : 700
Enter Value for Position 4 : 750
Enter Value for Position 5 : 901
Enter Value for Position 6 : 800
Enter Value for Position 7 : 820
Enter Value for Position 8 : 980
Enter Value for Position 9 : 860
Enter Value for Position 10 : 870
Enter Value for Position 11 : 930
User Input Values
Position : 0 , Sales Value : 900
Position : 1 , Sales Value : 200
Position : 2 , Sales Value : 800
Position : 3 , Sales Value : 700
Position : 4 , Sales Value : 750
Position : 5 , Sales Value : 901
Position : 6 , Sales Value : 800
Position : 7 , Sales Value : 820
Position : 8 , Sales Value : 980
Position : 9 , Sales Value : 860
Position : 10 , Sales Value : 870
Position : 11 , Sales Value : 930
How It Works
This C++ program demonstrates Read user input into Array. It first reads the required values as input, then walks through the data with a loop to compute the result, and finally prints the output shown in the Sample Output above.
- Declare the variables that hold the program’s data.
- Read the input values that the program will work with.
- Iterate over the data using a loop to apply the logic.
- Print the final result to the console so you can compare it with the sample output.
Try changing the input values and re-running the program to see how the output changes — this is the fastest way to understand how the logic behaves.
Related Pages
Continue learning with these related tutorials and programs:
- C++ Tutorials — Browse all C++ Tutorials.
- Understanding Array in C++ — Tutorial — array declaration, access and iteration.
- Passing Arrays to Functions In C++ — More in array in c.
Frequently Asked Questions
What does this C++ program do?
It is a C++ example program that demonstrates Read user input into Array, including the complete source code and the expected sample output.
How do I compile and run this C++ program?
Save the code in a `.cpp` file, compile it with `g++ filename.cpp -o program`, then run it with `./program` (or `program.exe` on Windows).
What concepts does this example use?
This example uses loops to iterate over data, arrays and reading user input, illustrating a common pattern in C++ programming.