c++ noob

Discussion in 'C++' started by baygross@gmail.com, Nov 12, 2005.

  1. Guest

    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 12, 2005
    #1
    1. Advertising

  2. Mike Wahler Guest

    <> wrote in message
    news:...
    >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
    Mike Wahler, Nov 12, 2005
    #2
    1. Advertising

  3. Guest

    thanks for the advice!
    -bg
    , Nov 12, 2005
    #3
  4. Greg Comeau Guest

    In article <>,
    <> 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?
    Greg Comeau, Nov 12, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Yttrium
    Replies:
    7
    Views:
    2,518
    Yttrium
    Oct 15, 2003
  2. xchris
    Replies:
    5
    Views:
    4,116
  3. NewsHound

    something of a noob question

    NewsHound, Jun 30, 2004, in forum: Perl
    Replies:
    1
    Views:
    417
    Jim Gibson
    Jun 30, 2004
  4. Chris Dunaway

    Really noob question about asp:Image control

    Chris Dunaway, May 27, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    1,032
    Chris Dunaway
    May 27, 2004
  5. haylow
    Replies:
    0
    Views:
    577
    haylow
    Jun 15, 2004
Loading...

Share This Page