Thread: Easy convert question

  1. #1
    Shake Zula- The Mic Rula!
    Join Date
    Sep 2004
    Posts
    69

    Easy convert question

    How do I convert a string containing say "12", into an integer 12 using C++??? I have to use the 'getline' function to grab data from a file, and 'getline' only lets you grab characters and strings.. any thoughts?

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    see FAQ -- I;m certain you will find the answer there. I could easily tell you, but you would miss out all the fun and extra knowledge of looking it up yourself. Happy hunting

    Hint: see std::stingstream

  3. #3
    Shake Zula- The Mic Rula!
    Join Date
    Sep 2004
    Posts
    69
    atoi! thats what i thought to use, but the way i had it set up wasnt working right... thanks anyways!

  4. #4
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by bcianfrocca
    atoi! thats what i thought to use, but the way i had it set up wasnt working right... thanks anyways!
    that is not a c++ solution. atol() is a C function, It will work, but will not satisfy your orginal request.
    Last edited by Ancient Dragon; 09-08-2005 at 08:50 PM.

  6. #6
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    It will work, but will not satisfy your orginal request.
    You're contradicting yourself. It'll work, but it won't work. In reality, it'll work just fine if the string represents a valid integer. atoi() and it's kin are all valid C++, even if they are inherited from C. It really doesn't matter whether the solution is unique to C++ or inherited as long as it works. If you're not keen on wrapping stringstream or using something like boost::lexical_cast then it's more awkward than atoi or strtol IMO.
    Just because I don't care doesn't mean I don't understand.

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by Narf
    You're contradicting yourself. It'll work, but it won't work. .
    No I did not contricit myself. The OP specifically stated a c++ solution. atoi() is a C solution. Just because c++ supports it doesn't make it a c++ solution any more than printf(), sprintf() gets() or any other C standard library functions.

    atoi() works the way it is documented to work. I personally would use it to make the conversion, but then I didn't ask the original question.

  8. #8
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    Just because c++ supports it doesn't make it a c++ solution any more than printf(), sprintf() gets() or any other C standard library functions.
    If the standard supports it, it's a C++ solution. Any other opinion is strictly based on what the person thinks a "C++ solution" should be. As long as we agree that it's impossible to be wrong in light of subjective opinions, we're both right and it's all good.
    Just because I don't care doesn't mean I don't understand.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. This is hopefully an extremely easy question...
    By rachaelvictoria in forum C Programming
    Replies: 2
    Last Post: 11-07-2005, 01:36 AM
  2. 4 easy question
    By Zeratulsdomain in forum C++ Programming
    Replies: 2
    Last Post: 10-15-2005, 10:43 PM
  3. easy newbie question
    By ioaz in forum C++ Programming
    Replies: 7
    Last Post: 09-24-2003, 03:58 PM
  4. Easy question about ports
    By Jaguar in forum Tech Board
    Replies: 4
    Last Post: 09-20-2003, 11:41 AM
  5. I have a 2 easy question about some homework?
    By correlcj in forum C Programming
    Replies: 3
    Last Post: 07-18-2002, 07:28 PM