Printing string array in For loop seems to print memory location, how to fix this?

I watched a tutorial on youtube on how to use for loop in c++ but the tutorial didn't show how to print string array using for loop. So I tried this but it just prints some weird characters that seem to be a memory location.

1
2
3
4
5
6
7
8
9
10
11
12
  #include <iostream>

int main()
{
    std::string mrRobot[5] = {"Eliot", "Darlene", "Whiterose", "Ollie", "Tyrell"};

    for(int i = 0; i <= 5; i++) {
        std::cout << mrRobot << std::endl;
    }

    return 0;
}
You missed the array index when displaying, so displayed the address of the first element - not values. Also you are missing an #include <string>. The for range is from 0 to < 5 as arrays as indexed starting at 0. so for a 5 element array, the valid indexes are 0 1 2 3 4

1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <string>

int main()
{
	std::string mrRobot[5] = {"Eliot", "Darlene", "Whiterose", "Ollie", "Tyrell"};

	for (int i = 0; i < 5; ++i)
		std::cout << mrRobot[i] << std::endl;
}

Oh! thanks it worked now
Topic archived. No new replies allowed.