pointers - Input Validation to make sure only number c++ -
ok, i'm trying @ using pointers i'm trying write input validation user input make sure isn't number handled correctly. when use isdigit() isn't working me. still exception when enter alphabet. suggestions? thanks. check out:
#include<iostream> #include<algorithm> #include<string> #include<cctype> using namespace std; void enternumbers(int * , int); int main() { int input = 0; int *myarray; cout << "please enter number of test scores\n\n"; cin >> input; //allocate array myarray = new int[input]; enternumbers(myarray,input); delete[] myarray; return 0; } void enternumbers(int *arr, int input) { for(int count = 0; count < input; count++) { cout << "\n\n enter grade number " << count + 1 << "\t"; cin >> arr[count]; if(!isdigit(arr[count])) { cout << "not number"; } } }
if test if (!(cin >> arr[count])) ... instead - isdigit(arr[digit]) tests if value of arr[digit] ascii code of digit [or possibly matches japanese, chinese or arabic (that is, arabic script typeface, not it's 0-9 our "arabic" ones) digit]. if type in 48 57, it's ok, if type 6 or 345, it's complaining not digit...
once have discovered non-digit, need either exit or clean out input buffer "garbage". cin.ignore(1000, '\n'); read next newline or 1000 characters, whichever happens first. annoying if has typed in million digits, otherwise, should solve problem.
you of course need loop read number again, until valid number entered.
Comments
Post a Comment