POV-Ray : Newsgroups : povray.off-topic : My first C++ program : Re: My first C++ program Server Time
4 Nov 2024 17:31:45 EST (-0500)
  Re: My first C++ program  
From: Slime
Date: 20 Sep 2008 01:07:51
Message: <48d48527$1@news.povray.org>
> The tutorial recommends the C function atoi() - which, being a C function, 
> is about as user-friendly as Windows 2. Much hair-pulling involving 
> pointers ensues.
>
> (It turns out that you can assign a string to a char*, but not the 
> reverse. Go figure. Anyway, the solution is simple enough...)

C strings aren't so bad. If you have a std::string, you can call its c_str() 
method to get a (const) pointer to the internal string, which is just a 
sequence of bytes. So, you can use

atoi( mystring.c_str() )

to convert to an integer. This is what I always use, though I see there are 
other ways to do it.

> (For one thing, AFAIK, there is *no way* to know whether atoi()
succeeded or failed; if it "fails", you get a zero. So was the string
actually a zero, or did the conversion fail??)

As I understand it, atoi's behavior is to return the integer represented by 
the longest sequence of characters starting at the beginning of the string 
that represent an integer, defaulting to 0. By this definition it's not 
possible to fail. =) Sometimes this is useful.

 - Slime
 [ http://www.slimeland.com/ ]


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.