By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,434 Members | 1,858 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,434 IT Pros & Developers. It's quick & easy.

c++ noob

P: n/a
I am taking an introduction to c++ at my school. We use the
Lawrenceville Press book and studio at my school. I just downloaded
Microsoft Visual Studio 2005 for experimenting away from school.

I am trying to write a function, (with Microsoft Visual Studio), to
turn a string into an integer. The LVP studio has a library, (whos
name a cant recall) that lets me turn a C++ string into a C string,
and then use atoi() to convert it to an integer. Visual Studio, as
far as i can tell, will not let me easily do this.

I have looked at the help file, but to no avail. The help file uses
characters to turn strings into intergers, and it confuses me. What is
the difference between:
string mystring="hello world" and char *mystring ="hello
world" ????
If anyone could give me a link or a simple explination to the
difference between these two types, and/or give me some help on
turning strings into integers, I would really appreciate it.

_BG

Nov 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

<ba******@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
I am taking an introduction to c++ at my school. We use the
Lawrenceville Press book and studio at my school. I just downloaded
Microsoft Visual Studio 2005 for experimenting away from school.
Visual Studio contains so many "bells and whistles"
that it's difficult for the novice to discover the
C++ translator buried deep within.

Try a simpler package, or read the documentation and
learn how to use the compiler from the command line.

I am trying to write a function, (with Microsoft Visual Studio), to
turn a string into an integer. The LVP studio has a library, (whos
name a cant recall) that lets me turn a C++ string into a C string,
The standard C++ function is a member of the std::string class:
c_str().
and then use atoi() to convert it to an integer.
'atoi()' does do that, but is inherently dangerous.
See the C FAQ for details.
Visual Studio, as
far as i can tell, will not let me easily do this.
All the components are there, albeit not easy for
a beginner to discern.

Also note that here we only discuss the C++ language,
not particular compiler packages.

I have looked at the help file, but to no avail.
A good book on C++ would be a much better alternative.
See the reviews at www.accu.org for ideas and suggestions.
The help file uses
characters to turn strings into intergers, and it confuses me. What is
the difference between:
string mystring="hello world" and char *mystring ="hello
world" ????
The first creates an object of the C++ standard library
type 'string'. The second creates a pointer object and
stores as its value the address of a string literal.
The string object is modifiable, the string literal
whose address the pointer contains is not (without
undefinded behavior).


If anyone could give me a link or a simple explination to the
difference between these two types, and/or give me some help on
turning strings into integers, I would really appreciate it.


#include <iostream>
#include <sstream>
#include <string>

int main()
{
std::string s("123");
std::istringstream iss(s);
int i(0);
iss >> i;
std::cout << i << '\n'; /* prints 123 */
return 0;
}

Alternatively, look up 'std::string::c_str()'
and 'strtol()' (declared by <cstdlib>)

-Mike
Nov 22 '05 #2

P: n/a
thanks for the advice!
-bg

Nov 22 '05 #3

P: n/a
In article <11*********************@o13g2000cwo.googlegroups. com>,
<ba******@gmail.com> wrote:
I am taking an introduction to c++ at my school. We use the
Lawrenceville Press book and studio at my school. I just downloaded
Microsoft Visual Studio 2005 for experimenting away from school.

I am trying to write a function, (with Microsoft Visual Studio), to
turn a string into an integer. The LVP studio has a library, (whos
name a cant recall) that lets me turn a C++ string into a C string,
and then use atoi() to convert it to an integer. Visual Studio, as
far as i can tell, will not let me easily do this.

I have looked at the help file, but to no avail. The help file uses
characters to turn strings into intergers, and it confuses me. What is
the difference between:
string mystring="hello world" and char *mystring ="hello
world" ????
If anyone could give me a link or a simple explination to the
difference between these two types, and/or give me some help on
turning strings into integers, I would really appreciate it.


Check out http://www.comeaucomputing.com/techtalk/#atoi
http://www.comeaucomputing.com/techtalk/#stringtostring
http://www.comeaucomputing.com/techtalk/#stringvsstring
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Nov 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.