I'm trying to make a loop that would only stop after a user has inputted a positive number, and would loop if a negative num or a char were typed in.
Code:
do{
printf ("Enter a Decimal number: ");
scanf ("%d", &a);
if (a > 0){
i = 1; /*'i' is sort of my
switch, changing its value breaks out of the loop*/
}
else{
printf ("Invalid Decimal number \n");
}
}while (i == 0); /*if anyone can suggest a
better way of doing this, I'd appreciate it... */
It seems to work fine on negative numbers, the user can try again, but it loops infinitely when put in a character...scan doesn't seem to work twice...