Like Tree2Likes

Convert a string to a char?

This is a discussion on Convert a string to a char? within the C++ Programming forums, part of the General Programming Boards category; Is it possible to convert a string to a char? I ask as I am inputting strings from a text ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    101

    Convert a string to a char?

    Is it possible to convert a string to a char?

    I ask as I am inputting strings from a text file, some of which are numbers so come in as "4" = a string.

    atoi will only convert char to int so I need to convert the string to a char in order to get an integer out of it - or is ther a function to convert string to integer?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,773
    Quote Originally Posted by JayCee++
    Is it possible to convert a string to a char?
    It depends on what you mean.

    Quote Originally Posted by JayCee++
    atoi will only convert char to int
    No, atoi converts a null terminated string to int.

    Quote Originally Posted by JayCee++
    I need to convert the string to a char in order to get an integer out of it - or is ther a function to convert string to integer?
    You can convert a std::string to an int. There are a few ways, one of which involves a stringstream, e.g.,
    Code:
    // str is a std::string
    std::istringstream ss(str);
    int num;
    ss >> num;
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    101
    laserlight you really are a gem.

    That is exactly what I need!

    I googled and googled for 'c++ string to integer' but all that ever comes up is 'atoi'.

  4. #4
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by JayCee++ View Post
    I googled and googled for 'c++ string to integer' but all that ever comes up is 'atoi'.
    No.. only the first result is atoi.. the other are excellent forum answers ..many using stringstreams and boost::lexical_cast .
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    2,760
    Also if you are compiling on a C++11 compatible compiler there are also the stoX series of functions. However for greater compatibility you would probably be better off using the stringstream solution, that works with all standard compliant C++ compilers.

    Jim
    manasij7479 likes this.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,630
    Know your terminology.
    char = a single character
    C++ string = std::string
    C-style string = array of chars with a '\0' as the last char
    A C++ string can be converted to a (constant) C-style string using the c_str() member function.
    A string always consists of multiple characters.
    rags_to_riches likes this.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by jimblumberg View Post
    Also if you are compiling on a C++11 compatible compiler there are also the stoX series of functions. However for greater compatibility you would probably be better off using the stringstream solution, that works with all standard compliant C++ compilers.

    Jim
    Wow.. can't imagine I missed this !

    Here's an idea....lets split this thread at this point and everyone interested make a post thereafter adding a lesser known (in your opinion) but handy feature of C++11.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,630
    Start a new thread instead.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Jan 2011
    Posts
    101
    Quote Originally Posted by jimblumberg View Post
    Also if you are compiling on a C++11 compatible compiler there are also the stoX series of functions. However for greater compatibility you would probably be better off using the stringstream solution, that works with all standard compliant C++ compilers.

    Jim
    I tried rg = stoi(ask); where 'rg' is an int and 'ask' is a string but got an error saying 'stoi' was not declared in this scope?

    I thought my compiler was C++11 compliant - mingw with Code::Blocks.

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    2,760
    How are you telling g++ to use C++0x? Normally you need to pass a parameter to the compiler to compile C++0x (-std=C++0x). Also it may not be available in the version of the compiler you are using, I am not sure what version of g++ added this feature but I believe it was available in version 4.4. This is why I noted that the stringstream method should be preferred since it has always been available in standard C++.

    Jim

  11. #11
    Registered User
    Join Date
    Jan 2011
    Posts
    101
    The compiler is set to 'Have g++ follow the coming C++0x ISO C++ language standard [-std=c++98]' - I thought that meant it was using the latest version.

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,630
    The command-line should be -std=c++0x, I believe.
    c++98 is the older standard, and c++11 (also known as c++0x) is the new one.
    So you should find that option.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Jan 2011
    Posts
    101
    Yes sorry I looked across a line, it does say c++0x and not 98.

  14. #14
    Registered User
    Join Date
    May 2010
    Posts
    2,760
    What is the version of your compiler?

    Jim

  15. #15
    Registered User
    Join Date
    Jan 2011
    Posts
    101
    in the help files it says 4.5/6

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Convert string to char or use 2d array of char?
    By simpleblue in forum C++ Programming
    Replies: 6
    Last Post: 09-25-2011, 05:00 PM
  2. Replies: 11
    Last Post: 06-16-2011, 11:59 AM
  3. Replies: 2
    Last Post: 09-12-2010, 09:15 AM
  4. Convert string to char[200]
    By maxorator in forum C++ Programming
    Replies: 4
    Last Post: 08-22-2005, 01:01 PM
  5. String Convert to Char ?
    By eko-eko in forum Windows Programming
    Replies: 2
    Last Post: 07-08-2002, 01:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21