c++ noob

B

baygross

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
 
M

Mike Wahler

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
 
G

Greg Comeau

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top