C++ For-Loop – Structure with Multiple Variable Initialization

c++for-loop

On the 2nd for-loop, I get the following error from gcc:

error: expected unqualified-id before 'int'

I'm not sure what I'm missing. I've looked over documentation for how a for-loop should look and I'm still confused. What's wrong here?

#include <iostream>
#include <vector>

int main() { 
std::vector<int> values; 

for (int i = 0; i < 20; i++) { 
  values.push_back(i); 
}   

std::cout << "Reading values from 'std::vector values'" << std::endl;
for (int i = 0, int col = 0; i < values.size(); i++, col++) {
  if (col > 10) { std::cout << std::endl; col == 0; }
  std::endl << values[i] << ' ';
  }
}

Best Answer

Try without the int before col.

for (int i = 0, col = 0; i < values.size(); i++, col++)

Related Question