how to convert a string into integer

Discussion in 'C++' started by priyanka, Jul 17, 2006.

  1. priyanka

    priyanka Guest

    Hi there,

    I want to convert a String into integer.

    I get the string from a file using :

    string argNum;
    getline(inputStream,argNum);

    I now need to convert argNum into integer.

    I tried to use

    int a = atoi(argNum);

    But I got errot message :error: cannot convert `std::string' to `const
    char*' for argument `1' to `int atoi(const char*)'

    And for the getline function, the argNum must be a string variable.

    Can anyone please tell me how to convert argNum into integer ?

    Thank you in advance,
    priya
     
    priyanka, Jul 17, 2006
    #1
    1. Advertising

  2. priyanka

    Noah Roberts Guest

    priyanka wrote:
    > Hi there,
    >
    > I want to convert a String into integer.
    >
    > I get the string from a file using :
    >
    > string argNum;
    > getline(inputStream,argNum);


    Why not:

    int x;
    inputStream >> x;
    >
    > I now need to convert argNum into integer.
    >
    > I tried to use
    >
    > int a = atoi(argNum);
    >
    > But I got errot message :error: cannot convert `std::string' to `const
    > char*' for argument `1' to `int atoi(const char*)'


    use c_str() to get the char* insides of a std::string.
    >
    > And for the getline function, the argNum must be a string variable.
    >
    > Can anyone please tell me how to convert argNum into integer ?
    >
    > Thank you in advance,
    > priya
     
    Noah Roberts, Jul 17, 2006
    #2
    1. Advertising

  3. priyanka

    red floyd Guest

    Noah Roberts wrote:
    > priyanka wrote:
    >> But I got errot message :error: cannot convert `std::string' to `const
    >> char*' for argument `1' to `int atoi(const char*)'

    >
    > use c_str() to get the char* insides of a std::string.
    >>


    You mean to get the _const_ char* insides of a std::string.
     
    red floyd, Jul 18, 2006
    #3
  4. priyanka schrieb:
    > Hi there,
    >
    > I want to convert a String into integer.
    >
    > I get the string from a file using :
    >
    > string argNum;
    > getline(inputStream,argNum);
    >
    > I now need to convert argNum into integer.
    >
    > I tried to use
    >
    > int a = atoi(argNum);
    >
    > But I got errot message :error: cannot convert `std::string' to `const
    > char*' for argument `1' to `int atoi(const char*)'
    >
    > And for the getline function, the argNum must be a string variable.
    >
    > Can anyone please tell me how to convert argNum into integer ?


    It's a FAQ:

    http://www.parashift.com/c -faq-lite/
    http://www.parashift.com/c -faq-lite/misc-technical-issues.html#faq-39.2

    --
    Thomas
     
    Thomas J. Gritzan, Jul 18, 2006
    #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. Azmie
    Replies:
    12
    Views:
    1,068,878
    Azmie
    Dec 30, 2003
  2. Replies:
    2
    Views:
    1,372
    Ravi Teja
    Jan 22, 2007
  3. Replies:
    8
    Views:
    696
    Marcelo Pinto
    Feb 1, 2006
  4. news
    Replies:
    2
    Views:
    1,138
  5. Randy Kramer
    Replies:
    12
    Views:
    408
    Robert Klemme
    Oct 25, 2007
Loading...

Share This Page