Validade input for integers, but a space is messing it up
This is the code I have so far:
Code:
#include <stdio.h>
#include <stdlib.h>
/* TODO: Fazer a validação para quando o usuário digita um espaço,
* pois está dando um resultado inesperado, como se um <Enter> estivesse
* sendo pressionado. */
/* TODO: Problem when the user a <Space> followed by <Enter> */
#define TRUEINT 1
void clear_screen ( void );
int main ( void ) {
int iNum1 = 0;
int iNum2 = 0;
clear_screen();
printf("A program that asks for two numbers and show them.\n\n");
/* First integer. */
printf("Enter an -integer-: ");
while (scanf("%d", &iNum1) != TRUEINT) {
while ( getchar() != '\n' ); // Limpa o buffer.
printf("Not an integer: ");
}
/* Second integer. */
printf("Enter the second -integer-: ");
while (scanf("%d", &iNum2) != TRUEINT) {
while ( getchar() != '\n' ); // Limpa o buffer.
printf("Not an integer: ");
}
printf("\t%d, %d\n", iNum1, iNum2);
return EXIT_SUCCESS;
}
void clear_screen(void) {
if (system("clear")) system("cls");
}
The problem is that when I hit <Space> followed by <Enter>, I get I new line in the prompt, and not the message that it is not an integer.
Any help would be appreciated. Thanks in advance.