#include<iostream>
#include<string>
usingnamespace std;
//declare the function
string GetArrayEntry()
{
string answer;
cout << "Please enter an entry into Array field ";cin >> answer;
return answer;
}
int main()
{
int wait;
//make the size of the array a constant size
constint sizeOfArray = 5;
//initiate the array
string MyArray[sizeOfArray];
//assign index the size of the actual array
int index = sizeof(MyArray) / sizeof(string);
// Input strings into an array of specified size Accending using the function
for(int i = 0; i < index; ++i)
{
MyArray[i] = GetArrayEntry();
}
// Retrieve strings from an array of specified size Accending
for(int i = 0; i < index; ++i)
{
cout << "The entry was " << MyArray[i] << endl;
}
// Retrieve strings from an array of specified size Deccending
for(int i = index; i > 0; --i)
{
cout << "The entry was " << MyArray[i] << endl;
}
cin >> wait;
This is wrong: for(int i = index; i > 0; --i) because index is outside the bounds of the array and the string at index 0 does not get printed.
Try for(int i = index-1; i > -1; --i)
By The way This is redundant (pointless):
1 2 3
//assign index the size of the actual array
int index = sizeof(MyArray) / sizeof(string);