So I have an IsNumber()
function which checks if an user input is a number or not, if it isn't the the program stops, however the function just isn't working for some reason.
Here is where it's implemented:
bool IsNumber(const char* pStr);
int main()
{
int user;
char decision;
char * str[256] = {user};
bool valid;
scanf("%d",&user);
clear_stdin(); // function to remove
sprintf(str, "%d", user); // to convert input into string so to validate number with function IsNumber
valid = IsNumber(str);
if (valid == false)
{
printf("Entered input is not a number, exiting now.");
exit(1);
}
}
And here is the function itself:
bool IsNumber(const char* pStr)
{
if(NULL == pStr || *pStr == "\0")
return false;
int dotCount = 0;
int plusCount = 0;
int minusCount = 0;
while (*pStr)
{
char c = *pStr;
switch(c)
{
case '.':
if (++dotCount > 1)
return false;
break;
case '-':
if (++minusCount > 1)
return false;
break;
case '+':
if (++plusCount > 1)
return false;
default:
if (c < '0' || c > '9')
return false;
}
pStr++;
}
return true;
}