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

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -