Problem with Vector of vectors

I'm trying to create a grid by user defined parameters,
I've done this by creating a vector of int vectors called grid, compiled of int vectors called rows.
I've then tried to fill them such that if a random generator between 1 and 10 comes back 10 it creates an obstacle (stored as value -2), otherwise it fills the element with the value -1.
however whenever i run my code, i get no error messages but all that is displayed is "press enter to continue" (my exit statement).
Any help would be much appreciated, and i've placed a copy of my code below



//Lees Algorithm vs A* Algorithm
// C Scrimgeour

#include <iostream>
#include <vector>
#include <stdio.h> /* printf, scanf, puts, NULL */
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
#include <cstdlib>
#include <chrono>


//Import things needed from the standard library
using std::cin;
using std::cout;
using std::endl;
using std::vector;
using std::chrono::duration_cast;
using std::chrono::milliseconds;
using the_clock = std::chrono::steady_clock;


//change parts of main to functions here


int main(int argc, char *argv[]) {

int userRows = 0;
int userColumns = 0;

//Get user values
cout << "Enter the number of rows in the grid\n";
cin >> userRows;
cout << "Enter the number of columns in the grid\n";
cin >> userColumns;
cout << "You are creating a " << userColumns << " x " << userRows << " grid.\n";

//create a vector of vectors user defined proportions for the grid
vector <vector<int>> grid;
for (int y = 0, y < userRows; y++) {
vector<int> row; //create an empty row
for (int x = 0; x < userColumns; x++) {
row.push_back(y * x); //Add a column to the row
}
grid.pushback(row); //Add the row to the main (grid) vector
}

//Fill grid vector
srand(time(NULL));
int randomValue = 0;

for (vector<vector<int>>::size_type y = 0; y < grid.size(); y++) { //loop through the y coords (rows)
for (vector<int>::size_type x = 0; x < grid[y].size(); x++) { //loop through the x coords (columns) for each row
randomValue = rand() % 10 + 1; //create random value for obstacles
if (randomValue > 9){ // if over 9 then create an obstacle (-2 value)
grid[y][x] = -2;
}
else{
grid[y][x] = -1; //else fill value as -1
}
}
}

//display the grid
for (vector<vector<int>>::size_type y = 0; y < grid.size(); y++){
for (vector<int>::size_type x = 0; x < grid[y].size(); x++){
cout << "|" << grid[y][x] << "| ";
}
cout << endl;
}

cout << "Press any key to continue\n";
cin.get();

return 0;
}
Last edited on
This code won't compile. Whatever you're running, it isn't this code.

for (int y = 0, y < userRows; y++) {
Here's the first line that won't compile.

When you try to build your code, look at the error messages it tells you.

Thank you, i believe i may have it figured out now.
I had a few syntax errors, "," where a ";" should have been as well as missing an underscore etc.

for (int y = 0; y < userRows; y++) {
vector<int> row; //create an empty row
for (int x = 0; x < userColumns; x++) {
row.push_back(y * x); //Add a column to the row
}
grid.push_back(row); //Add the row to the main (grid) vector
}

Last edited on
Topic archived. No new replies allowed.