• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 261
  • Last Modified:

input parser

i've to do a input parser for a small c++ project, but i've no idea, cos' i'm a beginner ;-)

it should know the difference between a float- and a char-input via "cin". so the pseudo code could look like that:

input via cin
if input = float
{do something}
if input = char
{do another thing}

any help is welcome!

1 Solution
Mayank SAssociate Director - Product EngineeringCommented:
Read the input as a string, and then check (scanning through a loop), if it is a valid number or not. If yes, then convert it to a numberic value using itoa () or ftoa ().... what-ever.


you probably mean strtol(), atoi(), strtod(), atod() or whatever :-)

Hmm...doesn't seem like a very difficult input parser to me. Do you recognize char data by a single quote character so 'a' is a char while just an   a    by itself is not? if so you must probably validate that it really is a character. Just a single single quote character by itself is not a char. In C and C++ a character is something that is enclosed in two single quote characters and in between can be a single char or a sequence of characters.

Actually C and C++ also allow literals like 'ab' etc but few people use those and they aren't really useful for much.

They are used in some cases for signatures etc though.

'ABCD' for example denotes an unsigned int value for compilers where sizeof(int) == 4.

Mayank SAssociate Director - Product EngineeringCommented:
Oh, sorry - yeah! atoi (), not itoa (), etc.


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Yes, you can read the value as a string, and loop through it. Check if each character is a numeral or alpha numeral, using the functions, isdigit(), isalpha() etc.

I would advice you to use strtod or strtof().

strto functions have one big advantage over ato functions:

1. The integer versions of them allow you to specify radix. Any value between 2 through 36 (both inclusive) and in addition the special value 0. 10 means decimal number, 16 means hex number, 8 is octal. 0 is special and translates to 8, 10 or 16 according to C and C++ rules. So 0xff would be hex while 0377 would be octal and 255 would be decimal and they all would translate to the same number.

2. They all take an argument which is a pointer to a pointer to a char and if that argument is given it is a pointer to a char pointer variable and that variable will be set to point to the first char that doesn't contribute to the number. I.e. it point to the end of the number.

if a string str holds the value "123.57g" and you define a variable like this:

char * end;

Then a call like this:

float f = strtof(str, & end);

will have f equal to the value 123.57 after the call and the variable end will point to the 'g'.

If the string was only a number the end pointer would point to the null byte. If the string hold a number followed by some blanks (space, tab etc) the end string pointer will point to the blank character.

atol(str) used to translate to strtol(str, NULL, 10) but after they added the rule about 0 for radix I believe most libraries translates atoi(str) to strtol(str,NULL,0). Some versions of libc provide atoi() as a separate function while others used to define it as a macro that simply call strtol(). Obviously, if you need a pointer to a function you need it as a function also you had to undef that macro. I believe most modern include files do not define it as a macro exactly for this reason and rather have a separate function that either call strtol() or which does the job itself (poor design).

In C++ the obvious choice would be to let atoi() be an inline function that called strtol() but I am not sure if any C++ versions of those include files does that.

Martin_KAuthor Commented:
strtold and strtod are returning only 5 digits after the comma... so when i try to convert 1.00001 it's ok, but 1.000001 returns just 1... how can i fix this?

err...are you sure?

I suspect the functions read the number correctly, it is the printing function that do not display them with more significant decimals than 5 and so the printing function round the result to 1.

Set 'precision' when you print the values and you should see a different story.

double x = strtod("1.000001", 0);
cout << x << endl;


Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now