Arrays of strings, for loop and why back to the beginning?

my program outputs this: it outputs the string:"I'm the best Heitz around",
then executes the for loop (un)successfully, because when the for loop is finished with it's output the program goes back to the beginning and once again outputs the string "I'm the best Heitz around".??? Once only. Why?
Nevermind sort of. I found the error. In the for loop there should be a < sign only, and not an = sign. Funny though that it looped back to the beginning of the program. I guess I'll post this in case someone else has the same thing happening.


here is my code:
#include <iostream>
using namespace std;

int main()
{
char* myStandard = "I'm the best Heitz around";
cout << myStandard << endl;

char* members[4] = {"Walter","Krumpf","Whetstein","Ms.Kraft"};

for(int i = 0; i <= 4; i++)
cout << members[i] << endl;

return 0;
}
That's odd, I don't see anything that could cause it to do that.

Although you should make your char* const char* instead.
I just tested in and it outputs everything as it should.
your for loop should be for(int i = 0; i < 4; i++). You're outside of the boundaries of the array when you run the loop when i == 4.

That has nothing to do with your problem, but I saw that and thought I should point it out.

edit: just saw that you already saw that, lol.... My fault for not reading.

I think that you just lucked out that the address at members[4] was the address for myStandard which is why it printed out again.
Last edited on
Topic archived. No new replies allowed.