visual studio 2010 - Check for non-numeric inputs in a C++ program -
how check non-numeric input using c++? using cin read in float value, , want check if non-numerical input entered via stdin. have tried use scanf using %d designator, output corrupted. when using cin, correct format, when enter, string such "dsffsw", infinite loop. commented code attempt capture float, , type cast string, , check if valid float, check comes false.
i have tried using other methods have found on message boards, want use scanf in c , not cin in c++. how do in c++? or in c if not feasible.
while (!flag) { cout << "enter amount:" << endl; cin >> amount; cout << "begin amount entered is: " << strtod(&end,&pend) << endl; //if (!strtod(((const char *)&amount), null)) { // cout << "this not float!" << endl; // cout << "i = " << strtod(((const char *)&amount), null) << endl; // //amount = 0.0; //} change = (int) ceil(amount * 100); cout << "change = " << change << endl; cout << "100s= " << change/100 << endl; change %= 100; cout << "25s= " << change/25 << endl; change %= 25; cout << "10s= " << change/10 << endl; change %= 10; cout << "5s= " << change/5 << endl; change %= 5; cout << "1s= " << change << endl; cout << "end amount entered is: " << amount << endl; } return 0;
}
int amount; cout << "enter amount:" << endl; while(!(cin >> amount)) { string garbage; cin.clear(); getline(cin,garbage); cout << "invalid amount. " << "enter numeric value amount:" << endl; }
Comments
Post a Comment