C++/help

Advertisement


Question
Supposing we have the following function (c language) that puts a REAL number in a table called input[]:
int getop(char input[])
{
int i,c
while((input[0]=c=getch())==' '|c=='\t';
input[1]='0';
if(!isdigit(c)&&c!='.')
return c;
i=0;
if(isdigit(c))
while(isdigit(input[++i]=c=getch()));
if c=='.'
while(isdigit(input[++i]=c=getch()));
input[i]='\0';
if(c!=EOF)
ungetch(c);
return NUMBER;
}
-how can i transform the above function so that it can only read integers?
-how can i transform the same function so that it can only read numbers in the hex-form (i mean that the base should be 16,for example B65F is the 46687 in the hex system
-could you help me in order to make some kind of a ishex() function that will check if the number given is given in a hex form?(just like the isdigit function, that checks out if the number given is a digit!
any help would be really helpful for me.thanks in advance!

Answer
Normally real numbers could apear like 3.567e-10 which lets you have very small and very large numbers. However in your case, it is a simplified version and so it can be easily used for integer. You only need to use the first part which reads the integer part of the real number. Delete the part that manipulates the fraction part (afte '.').
Working with hex numbers is very similar tio integer numbers. You only need to be able to detact the alphabetic digits (see below).
The is a function already implemented for this purpose. It is called isxdigit() and is in ctype.h. See the following microsoft link for more details:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Sam

Expertise

You may ask me about simple to complicated C/C++ programming methods/style and most like questions about network programming in C++.

Experience

I'm a professional programer in C++ and network/internet programming.

©2016 About.com. All rights reserved.