I am looking to have a user enter an 8 digit account number. The code I have seems to work for everything unless you have multiple letters, i.e.: 'bbb' as an input. If that happens, it runs the while loop 3 times, displaying the printf, but not asking for another input.
Any input on how to fix this, or a better way to do it is welcome!
Right now, I'm using:
#include <stdio.h>
int main()
{
int return_val = 0;
int account_number = 0;
int within_range = 0;
printf("Please enter your 8 digit account number:\n");
return_val = scanf("%d", &account_number);
getchar();
getchar();
if((account_number > 9999999) && (account_number < 99999999))
{
within_range = 1;
}
while ((return_val != 1) || (within_range != 1))
{
printf("Invalid account number. Account number must be 8 digits.\n");
printf("Please enter your 8 digit account number: \n");
//scanf("%d", &clear);
return_val = scanf("%d", &account_number);
getchar();
getchar();
if((account_number > 9999999) && (account_number < 99999999))
{
within_range = 1;
}
}
printf("Account #: %d", account_number);
}