|
@ -0,0 +1,23 @@ |
|
|
|
|
|
double testForNumber() { |
|
|
|
|
|
double num; |
|
|
|
|
|
char storage[25]; |
|
|
|
|
|
char* notANumber; |
|
|
|
|
|
|
|
|
|
|
|
do { |
|
|
|
|
|
//Emptys the standart output buffer to ensure that this function works in loops |
|
|
|
|
|
//and returns the value of num after every loop |
|
|
|
|
|
fflush(stdout); |
|
|
|
|
|
|
|
|
|
|
|
//fgets() reads the user input into the buffer storage (storage) |
|
|
|
|
|
//the if condition is true as long as the input isn't empty |
|
|
|
|
|
if (fgets(storage, sizeof storage, stdin) != NULL) { |
|
|
|
|
|
//strtod() tries to convert the input into a double |
|
|
|
|
|
//if there is anything but a number at any point of the array the pointer notANumber will point to this position |
|
|
|
|
|
num = strtod(storage, ¬ANumber); |
|
|
|
|
|
} |
|
|
|
|
|
//isspace makes sure that notANumber doesn't point to an empty space |
|
|
|
|
|
//the secount part makes sure that notANumber isn't pointing at 0 |
|
|
|
|
|
} while (!isspace(*notANumber) && *notANumber != 0); |
|
|
|
|
|
|
|
|
|
|
|
return num; |
|
|
|
|
|
} |