Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR C

how to prevent user from entering char when needing int in c

#include <stdio.h>
int main()
{
    int num=1;
    int numberRead = 0;
    do{
        printf("Enter a number: ");
        numberRead = scanf("%d",&num);
        while(numberRead != 1){
            printf("That is not a number.
");
           scanf("%*[^
]");  //or fflush(stdin);
            printf("Enter a number: ");
            numberRead = scanf("%d",&num);
        }
        printf("You entered %d
",num);
    }while(num != 0);

    return 0;
}
 
PREVIOUS NEXT
Tagged: #prevent #user #entering #char #needing #int
ADD COMMENT
Topic
Name
8+6 =