I was attempting to check if the input are all digits and no letters and symbols. I used isdigit() to check if the user had entered digits or letters, but it doesn't work, could anyone tell me why? The code I have written is below:
//I learned that isdigit() returns non-zero for true and 0 for false
int num = 123;
if(isdigit(num) != 0)
{
//do someting
}
Comments
: I was attempting to check if the input are all digits and no letters and symbols. I used isdigit() to check if the user had entered digits or letters, but it doesn't work, could anyone tell me why? The code I have written is below:
:
: //I learned that isdigit() returns non-zero for true and 0 for false
:
: int num = 123;
:
: if(isdigit(num) != 0)
: {
: //do someting
: }
:
[blue]isdigit macro works on characters in the range of '0' to '9', not integers. By definition, integers are numeric, they can't be anything else.[/blue]
[code]
#include
#include
int main()
{
char string[] = "123abc";
int len = strlen(string);
for(int i = 0; i < len; i++)
{
if( isdigit(string[i]))
printf("%d is a digit
", i);
else
printf("%d is NOT a digit
", i);
}
return 0;
}
[/code]
: : I was attempting to check if the input are all digits and no letters and symbols. I used isdigit() to check if the user had entered digits or letters, but it doesn't work, could anyone tell me why? The code I have written is below:
: :
: : //I learned that isdigit() returns non-zero for true and 0 for false
: :
: : int num = 123;
: :
: : if(isdigit(num) != 0)
: : {
: : //do someting
: : }
: :
:
: [blue]isdigit macro works on characters in the range of '0' to '9', not integers. By definition, integers are numeric, they can't be anything else.[/blue]
: [code]
: #include
: #include
: int main()
: {
: char string[] = "123abc";
: int len = strlen(string);
: for(int i = 0; i < len; i++)
: {
: if( isdigit(string[i]))
: printf("%d is a digit
", [red]string[i][/red]);
: else
: printf("%d is NOT a digit
", [red]string[i][/red]);
: }
: return 0;
: }
: [/code]
:
:
:
:
:
There isn't one -- you have to write your own function by looping through the string and checking each character.
: could stober plz write out that function for me? I have no idea how to wrtie it because I take the input into a "int" type variable. I can't check the input using loop because i am not taking input into a string. Thanx for help.
:
I already did -- read my previous post in this thread.
http://www.programmersheaven.com/c/MsgBoard/read.asp?Board=69&MsgID=226230&Setting=A9999F0001
: : could stober plz write out that function for me? I have no idea how to wrtie it because I take the input into a "int" type variable. I can't check the input using loop because i am not taking input into a string. Thanx for help.
: :
:
: I already did -- read my previous post in this thread.
:
:
: http://www.programmersheaven.com/c/MsgBoard/read.asp?Board=69&MsgID=226230&Setting=A9999F0001
:
:
perhaps you want the user to enter the number?
[code][size=3]
char buf[32];
...
printf("Enter your number: ");
fgets(buf, sizeof(buf), stdin);
for(i=0; buf[i] != ' '; i++)
{
if(isdigit(buf[i]))
action A
else
action B
}
...[/size][/code]
What action is taken depends on what you are expecting in the input.