c++ noob

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • baygross@gmail.com

    #1

    c++ noob

    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

  • Mike Wahler

    #2
    Re: c++ noob


    <baygross@gmail .com> wrote in message
    news:1131762219 .221245.25210@o 13g2000cwo.goog legroups.com...[color=blue]
    >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.[/color]

    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.
    [color=blue]
    >
    > 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,[/color]

    The standard C++ function is a member of the std::string class:
    c_str().
    [color=blue]
    > and then use atoi() to convert it to an integer.[/color]

    'atoi()' does do that, but is inherently dangerous.
    See the C FAQ for details.
    [color=blue]
    > Visual Studio, as
    > far as i can tell, will not let me easily do this.[/color]

    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.
    [color=blue]
    >
    > I have looked at the help file, but to no avail.[/color]

    A good book on C++ would be a much better alternative.
    See the reviews at www.accu.org for ideas and suggestions.
    [color=blue]
    > 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" ????[/color]

    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).
    [color=blue]
    >
    >
    > 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.[/color]

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

    int main()
    {
    std::string s("123");
    std::istringstr eam 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


    Comment

    • baygross@gmail.com

      #3
      Re: c++ noob

      thanks for the advice!
      -bg

      Comment

      • Greg Comeau

        #4
        Re: c++ noob

        In article <1131762219.221 245.25210@o13g2 000cwo.googlegr oups.com>,
        <baygross@gmail .com> wrote:[color=blue]
        >I am taking an introduction to c++ at my school. We use the
        >Lawrencevill e 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.[/color]

        Check out http://www.comeaucomputing.com/techtalk/#atoi


        --
        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?

        Comment

        Working...