Difference between revisions of "C++:Convert a String to a Number"

From Progzoo
Jump to: navigation, search
(New page: ==String to int== <question xmlns:htm="http://www.w3.org/1999/xhtml" lang="cpp" className="Demo"><prog> #include <iostream> #include <sstream> #include <string> using nam...)
 
Line 1: Line 1:
 
==String to int==
 
==String to int==
 +
{{topTenTips}}
 
<question xmlns:htm="http://www.w3.org/1999/xhtml" lang="cpp" className="Demo"><prog>
 
<question xmlns:htm="http://www.w3.org/1999/xhtml" lang="cpp" className="Demo"><prog>
 
#include &lt;iostream&gt;
 
#include &lt;iostream&gt;

Revision as of 21:17, 31 December 2007

String to int

The first example uses the function atoi (ASCII to integer), it works on the char [] or "C String" structure.

The to convert stdlib strings you can use the >> operator on stringstream class

The value "1234" is a string, you need to treat it as a number - to add 1, giving 1235.

[Font] [Default] [Show] [Resize] [History] [Profile]