C++ See If Argument Is Numeric -
i'm creating encryption/decryption program in c++, , use 3 user-provided numbers customize encryption. read isdigit() on cplusplus.com, , made function based on that:
bool is_numeric(char *string) { int sizeofstring = sizeof(string); int iteration = 0; bool isnumeric = true; while(iteration < sizeofstring) { if(!isdigit(string[iteration])) { isnumeric = false; break; } iteration++; } return isnumeric; }
however, doesn't seem work. whether give number, or non-numeric character, still returns false. wrong approach.
you computing sizeofstring
wrong. try instead.
bool is_numeric(char *string) { int sizeofstring = strlen(string); int iteration = 0; bool isnumeric = true; while(iteration < sizeofstring) { if(!isdigit(string[iteration])) { isnumeric = false; break; } iteration++; } return isnumeric; }
you may want add functionality check .
character well! right code returns true if string integer.
Comments
Post a Comment