I feel like I am missing something dreadfully obvious here, but I just can't seem to find the problem with my code. I am trying to use scanf to find if an input is an integer, and if it is, find the octal. If it is not an integer, then it simply prompts the user to enter again. For some reason, however, I can only seem to get the code to work if the reverse is the case, where an integer is not accepted and anything else is. Seems like it would be a simply problem to fix, but I just get infinite loops otherwise. Any help you guys could give would be appreciated.
#include <stdio.h>
enum state {success,fail,quit};
int status = fail;
int main(void)
{
int n;
char t;
do
{
printf("Enter a number between 0 and 32767: ");
if(scanf("%d%c", &n, &t) != 2 )
status = success;
}
while (status == fail);
if (status == success)
{
int oct1, oct2, oct3, oct4, oct5;
oct1 = ((((n / 8) / 8) / 8) / 8) % 8;
oct2 = (((n / 8) / 8) / 8) % 8;
oct3 = ((n / 8) / 8) % 8;
oct4 = (n / 8) % 8;
oct5 = n % 8;
printf("In octal, your number is: %d%d%d%d%d\n", oct1, oct2, oct3, oct4, oct5);
return 0;
}
}