?
Solved

input parser

Posted on 2003-03-27
7
Medium Priority
?
259 Views
Last Modified: 2007-12-19
hi,
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!

Martin
0
Comment
Question by:Martin_K
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 30

Expert Comment

by:Mayank S
ID: 8217808
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.

Mayank.
0
 
LVL 12

Expert Comment

by:Salte
ID: 8217886
Mayank,

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.

Alf
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8217910
Oh, sorry - yeah! atoi (), not itoa (), etc.

Mayank.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 2

Expert Comment

by:sateesh_babu
ID: 8218959
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.

0
 
LVL 12

Expert Comment

by:Salte
ID: 8219358
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.

Alf
0
 

Author Comment

by:Martin_K
ID: 8224004
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?

martin
0
 
LVL 12

Accepted Solution

by:
Salte earned 300 total points
ID: 8224059
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.

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

Alf
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question