Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 17th, 2005, 8:09 AM   #1
hipolito
Programmer
 
Join Date: Feb 2005
Posts: 41
Rep Power: 0 hipolito is on a distinguished road
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.

hipolito is offline   Reply With Quote
Old Feb 17th, 2005, 8:51 AM   #2
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
#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."
Infinite Recursion is offline   Reply With Quote
Old Feb 17th, 2005, 9:58 AM   #3
hipolito
Programmer
 
Join Date: Feb 2005
Posts: 41
Rep Power: 0 hipolito is on a distinguished road
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.
hipolito is offline   Reply With Quote
Old Feb 17th, 2005, 10:40 AM   #4
Hockeyman
Programmer
 
Hockeyman's Avatar
 
Join Date: Jan 2005
Location: Vancouver, Canada
Posts: 60
Rep Power: 4 Hockeyman is on a distinguished road
Send a message via MSN to Hockeyman
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. ~

Hockeyman is offline   Reply With Quote
Old Feb 17th, 2005, 11:01 AM   #5
hipolito
Programmer
 
Join Date: Feb 2005
Posts: 41
Rep Power: 0 hipolito is on a distinguished road
Cool nope

Nope, I tryed both with "&" and "%c", none works....

hipolito is offline   Reply With Quote
Old Feb 17th, 2005, 11:10 AM   #6
Hockeyman
Programmer
 
Hockeyman's Avatar
 
Join Date: Jan 2005
Location: Vancouver, Canada
Posts: 60
Rep Power: 4 Hockeyman is on a distinguished road
Send a message via MSN to Hockeyman
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. ~


Last edited by Hockeyman; Feb 17th, 2005 at 11:18 AM.
Hockeyman is offline   Reply With Quote
Old Feb 17th, 2005, 11:19 AM   #7
hipolito
Programmer
 
Join Date: Feb 2005
Posts: 41
Rep Power: 0 hipolito is on a distinguished road
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...
hipolito is offline   Reply With Quote
Old Feb 17th, 2005, 1:11 PM   #8
Hockeyman
Programmer
 
Hockeyman's Avatar
 
Join Date: Jan 2005
Location: Vancouver, Canada
Posts: 60
Rep Power: 4 Hockeyman is on a distinguished road
Send a message via MSN to Hockeyman
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. ~

Hockeyman is offline   Reply With Quote
Old Feb 17th, 2005, 1:41 PM   #9
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
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."
Infinite Recursion is offline   Reply With Quote
Old Feb 18th, 2005, 3:59 AM   #10
hipolito
Programmer
 
Join Date: Feb 2005
Posts: 41
Rep Power: 0 hipolito is on a distinguished road
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...
hipolito is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:20 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC