visual c++ - C++ cin.clear() and cin.ignore(...) issue -
so, have several fields fill in numerals. , if try fill in input letters (ex. 'noway' or 'gg1337' - makes , error , asks valid number (without letters ex. '13' or '1500000').
but there 1 problem, if start fill input numbers , add letters (for ex. '12nowshithappens'), jumps next field of input, thinking valid number, showing error in next input field.
here function code:
int appled() { cin >> appleds; while(cin.fail()) { cin.clear(); cin.ignore(numeric_limits<streamsize>::max(),'\n'); cout << "an arror occured!\nplease, enter valid number: "; cin >> appleds; } return appleds; }
if described wrong - here full code of test program :)
// exzerry presents #include <iostream> #include <conio.h> int apples; int fruit; int oranges; int x; int appleds; int orangeds; using std::cout; using std::cin; using std::endl; using std::numeric_limits; using std::streamsize; char newline = '\n'; bool ok; bool ok2; bool ok3; int appled() //function receive 'apples' input { cin >> appleds; while(cin.fail()) { cin.clear(); cin.ignore(numeric_limits<streamsize>::max(),'\n'); cout << "an arror occured!\nplease, enter valid number: "; cin >> appleds; } return appleds; } int oranged() //function receive 'oranges' input { cin >> orangeds; while(cin.fail()) { cin.clear(); cin.ignore(numeric_limits<streamsize>::max(),'\n'); cout << "an arror occured!\nplease, enter valid number: "; cin >> orangeds; } return orangeds; } int main() { ok = ok2 = ok3 = false; //some testing while(!ok2) //actual program loop { x = 0; // dropping program restart. //cout << "-----------------------" << endl; //cout << "debug mode: " << x << endl; //cout << "-----------------------" << endl; cout << "=====================================================" << endl; cout << "enter apples: "; apples = appled(); cin.clear(); cin.ignore(numeric_limits<streamsize>::max(),'\n'); //now have apples cout << "enter oranges: "; apples = oranged(); cin.clear(); cin.ignore(numeric_limits<streamsize>::max(),'\n'); //and have oranges cout << "\n=====================================================" << endl; cout << "calculating..." << endl; cout << "=====================================================" << endl; fruit = apples + oranges; cout << "in total have " << fruit << " fruit" << endl; cout << "=====================================================" << endl; cout << newline; //option go out or continue loop //if enter 1 - resets program, other char - exit cout << "go out? (1 - 'no', others - 'yeap'):" << " "; cin >> x; cout << newline; // ok2 = true; if (x == 1) { cout << "continue program..." << endl; //cout << "-----------------------" << endl; //cout << "debug mode: " << x << endl; //cout << "-----------------------" << endl; cin.clear(); cin.ignore(numeric_limits<streamsize>::max(),'\n'); ok = false; ok3 = false; continue; } if (x == 0) { cout << "shutting down app..." << x << endl; //cout << "-----------------------" << endl; //cout << "debug mode: " << x << endl; //cout << "-----------------------" << endl; break; } else { cout << "shutting down app..." << x << endl; //cout << "-----------------------" << endl; //cout << "debug mode: " << x << endl; //cout << "-----------------------" << endl; break; } } cout << "press key exit :d" << endl; getch(); return 0; }
you can make own facet parses character sequence add invalid. this:
class num_get : public std::num_get<char> { public: iter_type do_get( iter_type it, iter_type end, std::ios_base& str, std::ios_base::iostate& err, long& v) const { auto& ctype = std::use_facet<std::ctype<char>>(str.getloc()); = std::num_get<char>::do_get(it, end, str, err, v); if (it != end && !(err & std::ios_base::failbit) && ctype.is(ctype.alpha, *it)) err |= std::ios_base::failbit; return it; } };
now can install stream:
std::locale orig(std::cin.getloc()); std::cin.imbue(std::locale(orig, new num_get)); while (!(std::cin >> appleds)) { // input not entirely numeric } // input entirely numeric
Comments
Post a Comment