Currently, I have a loop in my program that has the format:
#include<stdio.h>
int main()
{
int n;
while(1)
{
printf("Enter a positive number, n");
scanf("%d",&n);
if(n>0)
{
break;
}
}
}
My intention in using the if(n>0) statement was to exit the loop if and only if the user enters a positive integer. However, if the user types in the character "g", for example, the loop will still break, as the ASCII value will be interpreted. How do I form this loop so that it breaks just when the user enters a positive integer value for n?
nis not initialised to anything. You are seeing undefined behaviour. See the answer and comments below.