So I've been working through C Primer Plus and I've run into a strange issue with one of the exercises.
The idea of the exercise was to make a basic calculator with menu and make it as bullet proof as possible.. Everything works fine except for the function designed to prompt and validate the calculator's input numbers.
Code:
float getNum(char prompt[])
{
float num;
char ch;
printf("%s", prompt);
while(scanf("%f", &num) != 1) //If input not a valid float
{
while((ch = getchar()) != '\n')
putchar(ch); // Print each character entered up to '\n'
printf(" is not a valid number.\n");
printf("%s", prompt);
}
while(getchar() != '\n') // Clear input buffer
continue;
return num;
}
What's weird is it seems to work for all input except for the following characters in parenthesis. (+-.ni) If +, -, or . are entered first the first character simply doesn't print.. ++ however prints a single +. n and i are even weirder though they seem to act like an escape and neither they nor the character which follow it will print... nnn or inn however prints a single n. I am completely baffled by this.. Does anyone have any idea what I seem to be missing?