I am very new to c programming and dont mean to be wasting anyones time but after trying to use the scanf function I am having some difficulties. I have created much more intracate programs (which isnt hard I know) but i havent used scanf, and I really cant see how this is going wrong.
Code:
int main(void){
int no, valid;
printf("\nHow many would you like?\n\n ");
if(scanf("%d",&no) != 1){ // Checking to see only one argument has been entered
printf("\nOnly one value.\n");
return (EXIT_FAILURE);
}
valid = scanf("%d",&no);
valid = ((valid != 0) && (valid != EOF));
if( !valid ){ // Checking to see the correct data type has been entered
printf("\nMust be an integer.\n");
return (EXIT_FAILURE);
}
return (EXIT_SUCCESS);
}
The error checks seem to not be working.
when I type in 'a 2 ' it prints 'Only one value' but when I type '2 a' it prints 'Must be an integer'. But in both instances I am expecting 'Only one value' to be printed.
Sorry for such a trivial problem but any help would be much appreciated
Cheers