Sign in to follow this  

Displaying contents of an array

This topic is 2544 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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, ", "));

Share this post


Link to post
Share on other sites
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..

Share this post


Link to post
Share on other sites

This topic is 2544 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this