-
problem with continue
I'm trying to write a program in which the user puts in a number, if its not a number you get prompted again to put in a number, if it is a number it just prints it back to you. simple, right?
Code:
#include<stdio.h>
#include<ctype.h>
int main()
{
int n;
printf("Enter a number:");
scanf("%d" , &n);
if(isalpha(n))
continue;
else if(isdigit(n))
printf("you said %d and that's sweet\n");
return 0;
}
anyway when I try to compile it(Borland if that matters) it says Misplaced continue in function main. how can I fix this? thanks guys
-
~
Why not just do:
Code:
#include<stdio.h>
#include<ctype.h>
int main()
{
int n;
printf("Enter a number:");
scanf("%d" , &n);
if(isdigit(n))
printf("you said %d and that's sweet\n");
return 0;
}
Or, if that doesn't work:
if (isdigit(n) && !isalpha(n))
printf("you said %d and that's sweet\n");
(dunno why you'd need that)
Also, in your printf statement, you don't print the number. It should be:
printf("you said %d and that's sweet\n", n);
-
Both of you are wrong. You do not use any of the "is*" functions on integers. Yes, they take integers, but they expect the range to be a valid range for 'char'. They translate that decimal value into the ascii value of the corresponding character, and tell you if that is a character. They are meant to be used like so:
Code:
char c = '1';
if( isalpha( c ) )
printf("%c is an alpha-numeric character.\n", c );
You do not pass it a value like 200 and
expect it to say that 200 is a number.
Code:
if( scanf("%d", &n ) == 1 )
printf("%d is a number.\n", n );
else
printf("You entered an invalid value.\n");
Quzah.