Displaying contents of an array

Started by
1 comment, last by PsyberMind 13 years, 3 months ago
This is really fundamental stuff, and the code is right, that I can see. But, being new to C++ I thought I'd throw this in here to see if I missed something.

Snippit:

int list[] = {7, 3, 32, 2, 55, 34, 6, 13, 29, 22, 11, 9, 1};cout << "The list of numbers is: ";		for (int x=0; x > 17; ++x)	{		 cout << list[x] << ", ";	}	cout << endl;


It compiles, and runs, but list[x] doesn't display. I know I'm missing something simple.

Advertisement
Your condition is "x > 17". But x starts at 0, which is not greater than 17. So the loop never executes.

You might consider using std::copy algorithm:
#include <algorithm>#include <iterator>std::copy(list, list + 17, std::ostream_iterator<int>(std::cout, ", "));
I knew it was something simple too, dangit.. changed to < and worked like a charm.

As far as the algorithm.. I'm not quite there yet, still getting the basics down, then will move on to the harder stuff.. I'm actually following a text book (a slightly older one) that a friend of mine sent me from his college days, pending Amazon's delivery of my Xmas present..

This topic is closed to new replies.

Advertisement