Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to distinguish between characters and numers. 1

Status
Not open for further replies.

Taxidriver

Programmer
Jan 15, 2002
79
IT
Hello guys,
can somebody tell me how to write a code that accepts numbers in input but stops reading when you type a character?
 
This depends on the size/type of number you are taking as input
but for instance, taking integers as input you can do...

while((c = getchar()) != EOF)
{
if((c & 0x00ff) == 'q')
exit(0)
else
...do somthing
}

Since "c" is a signed int (to check for EOF), I mask the upper bits to 0.
 
Hey,

You can also give change to scanf statement for this assignement, try this...

Code:
        char char_ar[20];
        scanf("%[0-9]",char_ar);

when you run this snippet and enter 1234ROY, char_ar will only store 1234, rejecting all characters after that.

Howzz that?

Roy.
user.gif
 
The problem is that my program should read numbers(integers) in order to calculate the average of these numbers. It should stop reading when it's introduced a non numerical character.
Can you be more explicative? Thanks
 
Well, I suppose you can start with the snippet above... ok, take the following code for eg:
Code:
char tmp_str[10]=" ";
int ctr=-1, total=0;

do 
{
	total+=atoi(tmp_str); //Converts the string to int.
	printf("\n\tEnter the next number:");
	ctr++;
	fflush(stdin);
}
while(scanf("%[0-9]",tmp_str));
//scanf returns 0 if nothing is read eg. in case of non-num.

printf("\nNumbers Entered:%d average is: %f",
        ctr,
       ((float)total/(float)ctr));

Well, I think that's it? B-)

Roy.
user.gif
 
RoyOBoy thanks for your prompt reply and sorry if I didn't write back again to tell you about the code that you kindly wrote.
It was fine but for one thing. The program stops reading also if you write a negative number. How should I modify it to make him accepts negative numbers as well as positive numbers?
Thank you.
 
The reason your program stops when entering a negative number is that you are entering the numbers into a char string. The program recognizes the minus sign as a ascii character.

way round it ?

char string[11], result[10], *ptr;
int count = 0;
printf("Enter a string up to 10 charcters :");
gets(string);
ptr = string;
while(*ptr)
{
if(!isdigit(*ptr))
{
if(*ptr != '-')
break;
}
else
result[count++]= *ptr;
}
result[count] ='\0';
printf("Numeric entries are %s", result);


Hoping to get certified..in C programming.
 
CORRECTIONS TO MY CODE ABOVE

#include <stdio.h>
#include <ctype.h>

void main ()
{
char string[11], result[11], *ptr;
int count = 0;

printf(&quot;Enter a string up to 10 charcters :&quot;);
gets(string);
ptr = string;
while(*ptr)
{
if(!isdigit(*ptr))
{
if(*ptr != '-')
break;
else
result[count]= *ptr;
}
else
{
result[count]= *ptr;
}
ptr++;
count++;
}
result[count] ='\0';
printf(&quot;Numeric entries are %s&quot;, result);
} Hoping to get certified..in C programming.
 
Hey guys,

bigtamscot, am afraid your point isn't correct. Well, what I am reading through scanf is a string so all it reads IS ascii.

Taxidriver, all you have to do is to play around with the format specifier string. just add a '-' in the wish list of characters you want it to read for you. See the code below:

Code:
scanf(&quot;%[
Code:
-
Code:
0-9]&quot;,tmp_str)

May be I'll give you some examples... if you wanted to accept only lower alpabets, your format string would be %[a-z].

Lower case alphabets and digits %[a-z0-9]

If the entry can be only vowels %[aeiou] thus only characters 'a','e','i','o','u' in any combination would be accepted.

Never imagined format strings could be this powerful? Tricky isn't it? :p

I hope the post was informative...;-)

Roy.
user.gif
 
Hey, no, I've never imagined that this format was so powerful.
Thanks Roy, the post was informative.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top