I'm trying to assure that my input string is correctly formated (correct format XXX-X-XXX ),but I don't wan't to use char temp[255] and cin.getline combination.
So far I've managed to "catch" all exceptions besides this one:
Enter registration plate: shjkf 22h 23jfh3kfh jkfhsdj h2j h2k 123-A-456
As assumed regPlate will get all the strings from the input including the correct formated one at the end and it will print the string.That's not correct. After reading the first string it should print Bad input and everything after it needs to be deleted.
I've tried to use cin.clear()
,cin.ignore()
,etc. in my if function but with no results.
void main()
{
std::string regPlate;
do
{
cout << "Enter registration plate: ";
cin >> regPlate;
if (regPlate.size() < 9 || regPlate.at(3) != '-' || regPlate.at(5) != '-' || regPlate.size() > 9)
{
cout << "Bad entry" << endl;
}
} while (regPlate.size() < 9 || regPlate.at(3) != '-' || regPlate.at(5) != '-' || regPlate.size() > 9);
cout << endl << regPlate << endl;
system("pause");
}
Would a while loop work, maybe something like: