string to int conversion? 
Author Message
 string to int conversion?

Hi,

    I'm curious if anyone knows of a way that might be simpler than what
I've thought of for converting a string into an int...  I'm trying to take
something like "(100)*.01"  and return an int...  except in there sometimes
I replace the names of variable to be switched in...  but if I can convert
that string into a value I figure that part won't cause problems...

So basically I'm wondering if there is any function built in that can do
such a conversion?

If not, is there a way better than creating a function that searches through
the string by every char and does a mess load of logical operations to
create a value?

sorry for the vagueness but I haven't started creating this part yet so I
don't have any code snipets to throw in...

Tia



Tue, 22 Feb 2005 17:31:10 GMT  
 string to int conversion?
would something like this work ?
int Int32.Parse(String s)
if your using ordinary c++ then you could use
int atoi(char* s)


Tue, 22 Feb 2005 23:49:43 GMT  
 string to int conversion?

Quote:
> Hi,

>     I'm curious if anyone knows of a way that might be simpler than
what
> I've thought of for converting a string into an int...  I'm trying to
take
> something like "(100)*.01"  and return an int...  except in there
sometimes
> I replace the names of variable to be switched in...  but if I can
convert
> that string into a value I figure that part won't cause problems...

You will need to write an expression parser, there is nothing built into
the language to do that for you.  For the actual conversion of a single
number in string format to an integer, you can simply do:

istringstream iss(str);
int i;
iss >> i;

But this requires that str is a number, like "102", it won't do
parenthesis or asterisks or whatnot.

Quote:
> So basically I'm wondering if there is any function built in that can
do
> such a conversion?

Nope, sorry.

Quote:
> If not, is there a way better than creating a function that searches
through
> the string by every char and does a mess load of logical operations to
> create a value?

Your best bet is to get a book about writing compilers.  They will have
all the details necessary to write your own expression parser.  If you
control the input format, you may want to look at prefix or postfix
notation instead, it will greatly simplify the parsing, but it will
dramatically change the look of your formulas.  Whether or not
pre/postfix notation will work for you is a matter of preference and
customer.

Ken



Wed, 23 Feb 2005 01:27:03 GMT  
 string to int conversion?
Sorry but I misunderstood what you wanted. But it seems  like you want to
parse and calculate string expressions. It seems like you want to have
something like the calculator in this book
http://www.relisoft.com/book/index.htm.

it parses the string 1 character at a time and creates a tree of nodes that
calculates the given expression. Unless there is a better builtin parser
this way you can get the work done at least.



Wed, 23 Feb 2005 04:28:02 GMT  
 string to int conversion?

Quote:

> [...]
> Your best bet is to get a book about writing compilers.  They will have
> all the details necessary to write your own expression parser.  [...]

  Mhmm. Now that _does_ look a lot like a compiler
  constuction course's homework, in which case this
  pointing to a compiler building book might be
  good or not. But if it's just a plain and simple
  one-time task for an app, buying a book about
  compiler construction might be a little bit of an
  overkill...

  IIRC, the current edition of Stroustrup's "The
  C++ Programming Language" does have such a little
  desk calculator as an introductionary example at
  the beginning of the book. This book should be
  a "must be on the desk" for any C++ programmer
  anyway, so if you don't have it, it won't hurt
  to buy it.
  (And if that _is_ a homework, you definately
  shouldn't simply re-type this example. I'd be
  very surprised about an instructor which gives
  such homeworks in C++ and can't tell this piece
  when it comes in as a "homework solution".)

Quote:
> Ken

  Schobi

--

I'm hschober at gmx dot de



Thu, 24 Feb 2005 21:13:51 GMT  
 string to int conversion?

Quote:

> > [...]
> > Your best bet is to get a book about writing compilers.  They will
have
> > all the details necessary to write your own expression parser.
[...]

>   Mhmm. Now that _does_ look a lot like a compiler
>   constuction course's homework, in which case this
>   pointing to a compiler building book might be
>   good or not. But if it's just a plain and simple
>   one-time task for an app, buying a book about
>   compiler construction might be a little bit of an
>   overkill...

I wasn't necessarily recommending purchasing a compiler book - if it is
a one time thing, I'm sure a local library will have a book on
compilers, or at least can order one in from a larger library at no
charge.

That said, it's not a bad idea to have a book on compilers even if you
aren't writing one - a lot of the concepts are applicable elsewhere, and
knowing what the compiler is doing will help you better understand using
the compiler.

But you're right, if it's just a small, one time project, it's probably
overkill.

Ken



Fri, 25 Feb 2005 05:07:01 GMT  
 string to int conversion?

Quote:

> [...]
> I wasn't necessarily recommending purchasing a compiler book - if it is
> a one time thing, I'm sure a local library will have a book on
> compilers, or at least can order one in from a larger library at no
> charge.

  If I can show my boss that it would save three
  hours of work he would always buy a book. But
  usually my problem is finding the time reading
  it. ;^>

Quote:
> That said, it's not a bad idea to have a book on compilers even if you
> aren't writing one - a lot of the concepts are applicable elsewhere, and
> knowing what the compiler is doing will help you better understand using
> the compiler.

  That is right, of course. Especially this seems
  to help me to understand the really bad error
  messages that C++ compilers tend to issue on
  some very silly mistakes (like forgetting a
  semicolon).

Quote:
> [...]
> Ken

  Schobi

--

I'm HSchober at gmx dot de



Fri, 25 Feb 2005 22:54:59 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. File IO and string to int conversion

2. string to int conversion

3. string to int conversion

4. Newbie Question|string to int conversion

5. Problem with conversion of string to int.

6. int to string conversion?

7. Unicode string conversion to int/float ?

8. RogueWave : int-to-string conversion

9. Type conversion (String to Int)

10. String conversion and double-to-string conversion

11. String conversion and double-to-string conversion

12. String conversion and double-to-string conversion

 

 
Powered by phpBB® Forum Software