Using as much as your code as I could:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAXLINE 100
int main(void)
{
char line[MAXLINE];
int n;
int i;
int length;
int error = 0;
do
{
error=0;
printf(" Enter a positive integer: \n");
printf("\n");
fgets(line, MAXLINE, stdin);
sscanf(line, "%d", &n);
length = strlen(line);
if ( line[length-1] == '\n' )
{
--length;
line[length]='\0';
}
for (i = 0; i < length; ++i)
{
if(!isdigit(line[i]))
{
error = 1;
break;
}
}
if (error)
{
printf("\n Try again!");
}
else
{
printf("\n Looks like an integer to me. Thanks!\n");
}
} while (error);
return 0;
}
The conditional to remove the newline is just something I like to do and not required. Though if you do get rid of it make sure to change the for loop to length - 1.
Empty if statements are kinda pointless. If you aren't going to do anything when its true and are going to do something when its false you can use negation to simplify it.
Also once you have found out that their is a character in the stream there is no real point in checking the rest of it.