In a separate sentence want to identify and print out each number from an array. I am having trouble finding a way to print in each sentence the identification of "first, "second", etc in each sentence. Have tried to use an array of strings to do this. What is the best way to accomplish this?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
int main()
{
constint SIZE = 5; // Size of the array
int numbers[SIZE] = {10, 20, 30, 40, 50}; // Array of integers
int count; // Counter variable
string ordinal[SIZE] = { "first", "second", "third", "fourth", "fifth"};
for (count = 0; count < SIZE; count++)
//print out each number in a separate sentence identifying the order of each number.
cout << "The" << ordinal[count] << "element in the array is " << *(numbers + count) << "." << endl;
return 0;
}
You pretty much have it, just change *(numbers + count) to numbers[count]. Oh yeah, and change the variable name of count, it has a predefined purpose. And one last thing, add spaces between 'The' and " as well as " and 'element'
thanks for the advice. Below are changes.
It prints out everything but the ordinal names after "The".
I still get an error message as follows: Error 1 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) 17 (referring to cout << line)
my current code with changes:
int main()
{
const int SIZE = 5; // Size of the array
int numbers[SIZE] = {10, 20, 30, 40, 50}; // Array of integers
int index; // Counter variable
string ordinal[SIZE] = { "first", "second", "third", "fourth", "fifth"};
// To print out each element of the numbers array in a seperate sentence in order.
for (index = 0; index < SIZE; index++)
cout << "The " << ordinal[index] << "element in the array is " << numbers[index] << " ." << endl;