Based on this answer, I've written the following code
#include <iostream>
#include <vector>
#include <cstddef>
#include <limits>
int main()
{
std::cout << "Enter x and y size, followed by enter: ";
std::size_t nrOfRows, nrOfCols;
std::cin >> nrOfRows >> nrOfCols;
// initialize dynamic array of arrays
std::vector<std::vector<char>> data(nrOfRows,
std::vector<char>(nrOfCols, 'O'));
// print array
for (std::size_t rowNr = 0; rowNr < nrOfRows; rowNr++)
{
std::cout << "Row " << rowNr << ": ";
for (const auto& el : data[rowNr])
std::cout << el << " ";
std::cout << std::endl;
}
std::cout << "Press enter to continue: ";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
Compiled with VC++ 14.1 or Visual studio 2017 v15.7.4.
After the first prompt I enter e.g. "3 5" and enter. Then the program just rolls through and exits. E.g. it outputs the strings and does not wait for the final user input (enter) at std::cin.ignore()
.
What did I miss?
edit
For the down-voters/nay sayers. This code does work as described.
#include <iostream>
#include <limits>
int main()
{
std::cout << "Press enter to continue: ";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}