From b35930f9c55928361b96e762a32ba94e67a1a855 Mon Sep 17 00:00:00 2001 From: lukazieg <2003lukas.zie@gmail.com> Date: Sat, 3 Feb 2024 11:24:06 +0100 Subject: [PATCH] added testForNumber.c --- src/main/c/testForNumber.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/main/c/testForNumber.c diff --git a/src/main/c/testForNumber.c b/src/main/c/testForNumber.c new file mode 100644 index 0000000..89784ed --- /dev/null +++ b/src/main/c/testForNumber.c @@ -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; +} \ No newline at end of file