Write a C++ program to accept five integer values in an array using pointer offset notation.
#include <iostream>
int main()
{
const int ARRAY_SIZE = 5;
int data[ARRAY_SIZE];
std::cout << "Please enter " << ARRAY_SIZE << " integers: ";
for(int i=0; i<ARRAY_SIZE; ++i)
{
std::cin >> *(data + i);
if(!std::cin)
{
std::cout << "Bad input\n";
return 1;
}
}
std::cout << "Array data: ";
for(int i=0; i<ARRAY_SIZE; ++i)
{
std::cout << *(data + i) << (i < ARRAY_SIZE - 1 ? " " : "\n");
}
return 0;
}
Comments
Leave a comment