I want to make a vector of doubles from user input. These lines of code do generally what I want them to do, except for two cases, and if there are more please let me know. The two cases are if there are two or more decimal places, and if there is one or more spaces. The second decimal acts as a space. So if the user inputs this: 0.2.0.1, or 0.2 0.1, I would get two decimals for both instances and they would be 0.2 and 0.1. Please help me on this. I want to make sure I am doing this simple task correctly. I am doing this in C++.
Any other suggestions are more than welcome.
for(int i = 0; i <= ARRSIZE; i++)
{
cout << "Please enter a decimal value: ";
if (cin >> sumScores)
{
scoresVector.push_back(sumScores);
cin.clear();
cin.ignore();
}else
{
cout << "This is not a double\n";
scoresVector.push_back(0);
cin.clear();
cin.ignore();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
}
There are better solutions to handle linewise inputs, but they all require some additional code. The easiest way is reading a line using
std::getlineand analyze them with a stringstream.