![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Feb 2005
Posts: 41
Rep Power: 0
![]() |
number or letter???
hello all,
I am doing a little programming on C and I need a little help. User can enter in a field a number (code) or letter (name), how do find out if the variable he typed in was letter or number??? Thank you all. ![]() |
|
|
|
|
|
#2 |
|
Programming Guru
![]() ![]() ![]() |
#include <ctype.h>
isdigit() - tests if character is a digit isalpha() - tests for alphabetic character
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Feb 2005
Posts: 41
Rep Power: 0
![]() |
thx, but.....
Well, thx a lot, it was realy what I was looking for, but still I got an error when I enter a number... segmentation error...
This is what I have: printf("\nType number or name: "); scanf("%d", value); if (isdigit(value)) printf("\nNumber"); else printf("\nLetter"); What should I do? Thx again. |
|
|
|
|
|
#4 |
|
Programmer
|
The scanf function uses pointers (aaaaaarrrgggghhhh)
try: printf("\nType number or name: ");
scanf("%d", &value);
if (isdigit(value))
printf("\nNumber");
else
printf("\nLetter");I'm not sure if this'll make any difference, but I'd use %c in the scanf to read in a char, instead of %d to read in a decimal integer - although that could just be my preference rather than any C rule.
__________________
~ You know, Hobbes, some days even my lucky rocketship underpants don't help. ~ read my blog @ My Lucky Rocketship Underpants
|
|
|
|
|
|
#5 |
|
Programmer
Join Date: Feb 2005
Posts: 41
Rep Power: 0
![]() |
Nope, I tryed both with "&" and "%c", none works....
![]() |
|
|
|
|
|
#6 |
|
Programmer
|
how did you declare your variable "value"?
Another thing I've just noticed - you have in your printf for the user to enter a number or a name - but your scanf isn't set up to read strings. If they enter a full name, it'll just read the first character and leave the rest in the input buffer. You can fix it by asking the user to enter a number or a letter (easiest), or declaring value as a string and using %s in scanf to read (trickier).
__________________
~ You know, Hobbes, some days even my lucky rocketship underpants don't help. ~ read my blog @ My Lucky Rocketship Underpants
Last edited by Hockeyman; Feb 17th, 2005 at 11:18 AM. |
|
|
|
|
|
#7 |
|
Programmer
Join Date: Feb 2005
Posts: 41
Rep Power: 0
![]() |
oh oh
I have tryed both int value and char value.....
I think the problem is scanf, it might not accept letter as input. oh my god, would that be it??? I also tryed the atoi(value)...still shiiitous... |
|
|
|
|
|
#8 |
|
Programmer
|
scanf will accept anything that is typed as input...might not interpret it like you think it's going to though.
Assuming you've included the correct libraries etc, that code should work. There may be a problem somewhere else in your code. In an earlier post you said it wasn't accepting numbers, your last post said letters - which isn't it accepting? What exactly is the error message you're getting? Does it compile and give a run-time error, or will it not even compile?
__________________
~ You know, Hobbes, some days even my lucky rocketship underpants don't help. ~ read my blog @ My Lucky Rocketship Underpants
|
|
|
|
|
|
#9 |
|
Programming Guru
![]() ![]() ![]() |
Here ya go... thnx for showing effort.
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
int main (void)
{
char value;
printf("\nType number or name: ");
scanf("%d", &value);
if (isalpha(value))
{
printf("\nLetter");
}
else
{
printf("\nNumber");
}
system("PAUSE");
return 0;
}
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
|
|
#10 |
|
Programmer
Join Date: Feb 2005
Posts: 41
Rep Power: 0
![]() |
Well here it goes!
This is what I did:
void routine() char value; printf("\nEnter a value: "); scanf("%d", &value); if (isalpha(ponto)) printf("\nLetter"); else printf("\nNumber"); os_exit(0); Just what Infinite did, well even letter or a number shows Number on my screen... I guess difining it as char, whaever I type will aways be a string... and Hockeyman, the program compiles just well, and the error shown is when I define the value as int and type a letter... so I must use the "%d" on scanf to get it work for both letters and number, but still the if clause will always go on ELSE option... |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|