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
//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;
}
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
}