Why I cannot use this methode to flush out the invalid input.
I wanted to have the input part inside the while loop, unlike the one the FAQ show on the board(Inside the while condition).
Code:#include <iostream> #include <cctype> #include <fstream> #include <limits> using namespace std; struct inventory_items { int code,quantity,reorder_level; char name[100], units[100]; double price; }; int main() { int counter=0; inventory_items s; while (counter==0) { cout<<"Enter the item code.\n"; cin>>s.code; cin.get(); if(!isdigit(s.code)) { cout << "\nInvalid input" <<endl; cin.clear(); cin.ignore(std::numeric_limits < int >::max(), '\n'); } else { cout<<"You enter : "<<s.code<<endl; counter=1; } } return 0; }



LinkBack URL
About LinkBacks


