Why doesn't this work?

Any obvious reason why this wouldn't work in visual studio 2013?
1
2
3
4
5
6
7
8
9
10
11
12
13
  #include <iostream>

using namespace std;

char words[] = "Test, The, Quick, Brown, Elephant, Jumps, Lightly, Around, a, Box";

int main() {
	for (int i = 1; strlen(words); i++){
		cout << words[i] << endl;
	}

	system("Pause");
}
It works just fine.
It doesn't compile because you haven't included all the necessary headers:

1
2
3
#include <cstring>   // strlen()
#include <iostream>  // cout
#include <stdlib>    // system() 
closed account (E0p9LyTq)
Gector wrote:
Any obvious reason why this wouldn't work in visual studio 2013?

Compiles and works with VS 2015 Community when the for statement is rewritten:

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

char words[] = "Test, The, Quick, Brown, Elephant, Jumps, Lightly, Around, a, Box";

int main() 
{
   for (int i = 0; i < strlen(words); i++)
   {
      std::cout << words[i];
   }

   std::cout << "\n";
   system("Pause");
}


I also changed the formatting to make it easier to read (no std::endl after every character written).

Your for statement, for (int i = 1; strlen(words); i++) exceeds the array's boundary.

Duoas wrote:
It doesn't compile because you haven't included all the necessary headers:

#include <stdlib> // system()

Wouldn't that header be #include <cstdlib> or #include <stdlib.h> ?

I didn't include any headers other than #include <iostream> and the code compiled with VS 2015. Another non-standard quirk of Visual C++?
Another non-standard quirk of Visual C++?

Not necessarily, an implementation is free to include other header files that are required for it's implementation.

However as a programmer you should never rely on some implementation detail to include required header files.

Topic archived. No new replies allowed.