Hello every body

I'm trying to iterate through a list for a certian number of times using a for loop.
below is the current code I've written, but this will iterate through the list until it ends.

list<FileInfo*>::iterator  p = currentlist.begin();
  while(p != currentlist.end()) {

    cout << (*p)->remainingtime << " ";
    (*p)->remainingtime--;
    cout << (*p)->remainingtime << " ";


if ((*p)->remainingtime==0){
    p = currentlist.erase(p);
}
}    
cout << "\n\n";

How many times do you want it to loop?
You can set up a counter to break after that many times...

#include <iostream>
#include <list>
using namespace std;

int main(void)
{
   list<string> lst_str;
   lst_str.push_back("once");
   lst_str.push_back("upon");
   lst_str.push_back("a");
   lst_str.push_back("time");
   lst_str.push_back("there");
   lst_str.push_back("were");
   lst_str.push_back("three");
   lst_str.push_back("bears");
   //
   list<string>::iterator it = lst_str.begin();

   for(int i=0 ;i<4; i++)
   {
      cout << (*it++).c_str() << endl;
   }

   return 0;
}

Assuming I'm understanding this correctly, what you need to do is combine a regular for() loop conditional with the test for the end of the loop as you have it above:

for (int i = 0; (i < iterations) && (p != currentlist.end()); i++)
{
  // code goes here
}

The reason you need both is for the case where the list is shorter than the number of iterations.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.