Read array element from keyboard and output
#include <iostream>
int main()
{
int myArray[5];
for (int i=0; i<5; i++) // 0-4
{
std::cout << "Value for myArray[" << i << "]: ";
std::cin >> myArray[i];
}
for (int i = 0; i<5; i++)
std::cout << i << ": " << myArray[i] << "\n";
return 0;
}
Value for myArray[0]: 1
Value for myArray[1]: 2
Value for myArray[2]: 3
Value for myArray[3]: 4
Value for myArray[4]: 5
0: 1
1: 2
2: 3
3: 4
4: 5
Read array element one by one from keyboard
#include <iostream>
#include <cctype>
using std::cout;
using std::cin;
using std::endl;
int main() {
int height[10];
int count = 0;
char reply = 0;
do {
cout << endl
<< "Enter a height as an integral number of inches: ";
cin >> height[count++];
// Check if another input is required
cout << "Do you want to enter another (y or n)? ";
cin >> reply;
} while(count < 10 && std::tolower(reply) == "y");
return 0;
}
Enter a height as an integral number of inches: 12
Do you want to enter another (y or n)? n