I have written small C++ console application and this is source code :
#include<stdio.h>
#include<locale.h>
#include<ctype.h>
#include<stdlib.h>
void main()
{
setlocale(LC_ALL, "turkish");
int a,b,c,d;
printf("first number: ");
scanf("%d", &a);
printf("second number: ");
scanf("%d", &b);
c = a+b;
printf("Sum: : %d\n", c);
}
As you can see i’m requesting two numbers from user and than summing them. But i want to add a control which check number who enterede by user is integer?
I’ll check number which typed by user and than if number isn’t really a integer i will echo an error. I’m using this after every scanf but it’s not working very well.
if(!isdigit(a))
{
printf("Invalid Char !");
exit(1);
}
In shortly, on a scanf action, if user type “a” it will produce an error message and program stop working. If user type a number program will continue
scanfdoes that validation for you. Just check the return value fromscanf.