I\'m writing a program that prompts the user for:
First part is fine, I create a dynamica
Victor's answer works but does more than is necessary. You can just directly call ignore() on cin to skip the commas in the input stream.
What this code does is read in an integer for the size of the input array, reserve space in a vector of ints for that number of elements, then loop up to the number of elements specified alternately reading an integer from standard input and skipping separating commas (the call to cin.ignore()). Once it has read the requested number of elements, it prints them out and exits.
#include
#include
#include
#include
using namespace std;
int main() {
vector vals;
int i;
cin >> i;
vals.reserve(i);
for (size_t j = 0; j != vals.capacity(); ++j) {
cin >> i;
vals.push_back(i);
cin.ignore(numeric_limits::max(), ',');
}
copy(begin(vals), end(vals), ostream_iterator(cout, ", "));
cout << endl;
}