I want the user to enter only two integers, and no more than two. The user should enter two integers separated by spaces. So, for example:
- The valid input is: 1 2
- Invalid input: 1 2 3
#include<stdio.h>
int main(){
int a;
int b;
printf("Enter input:\n");
int numbers = scanf("%d %d", &a, &b);
if(numbers != 2){
printf("Invalid input.\n");
}
else{ printf("First: %d Second: %d\n", a, b);
} return 0;
}
If I enter more than 2 integers, the program accepts the first 2 integers and ignores the third. In this case, I want to print an error, but the program still ends. So my question is, what should I do to achieve my needs?
thanks.