Convert from string to int.

This is a discussion on Convert from string to int. within the C++ Programming forums, part of the General Programming Boards category; Hi folks, I know this is something that is asked a lot, but I've been reading lots about it and ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    12

    Question Convert from string to int.

    Hi folks,
    I know this is something that is asked a lot, but I've been reading lots about it and am still none the wiser.

    I am reading from file - most values are strings but the odd one is an int. I'm happy reading the int's as strings but I need to convert them into int's so that I can work with them.

    At the moment, I have a variable of string type called Length that needs to be an int - thats all!

    I've tried many different methods but am failing miserable with all of them!.

    I notice a lot use the #include <sstream> however, when I try to include it I get "CdPlayer.h:6: sstream: No such file or directory"
    I've also tried the atoil functions without joy.

    Thank you!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    using namespace std;
    
    int main ( ) {
        string a = "10";
        istringstream b(a);
        int c;
        b >> c;
        cout << "Result=" << c << endl;
    }
    Works OK with g++
    Which compiler are you using?

    AFAIK, sstream is the new name, so if you have something really old (compiler wise), you may have some problems.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    sprintf()?

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by Queatrix View Post
    sprintf()?
    Did he not say string to int, not int to string ? :P

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    197
    I would use the function "atoi".

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    A stringstream is a bit better than atoi, especially if you're not 100&#37; sure your string is an integer.

    If the compiler is too old to use sstream, then perhaps it is time for a new compiler.

    If a new compiler is not allowed, then show your attempt using atoi.

  7. #7
    Registered User
    Join Date
    May 2007
    Posts
    12
    My compiler is DevC++ version 4.

    I'm afraid I've deleted all my attempts at atoi because they didn't work! Although if I remember correctly, the error that I got when using Atoi was that it couldn't convert a string to a char* bizarrely.. I don't really know why this is.. I'll try and recreate the error for you. Do you have any simple atoi segments that I could try and use to make sure I'm not doing anything stupid?

    But yes, include sstream doesn;t work.

    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    using namespace std;
    
    int main ( ) {
        string a = "10";
        istringstream b(a);
        int c;
        b >> c;
        cout << "Result=" << c << endl;
    }
    Something like that would be ideal for my needs!!

    Thanks for your responses.

    I've just recreated the atoi error, it is :
    Code:
    68 CdPlayer.h  cannot convert `line' from type `string' to type `const char *'
    the line of code for the conversion is
    Code:
     Leigh = atoi(line);
    Is it because the atoi method requires to be passed a char * and not a string string?!
    Last edited by LeighRogers; 05-09-2007 at 12:13 PM.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,634
    My compiler is DevC++ version 4.
    If your IDE is Dev-C++ version 4, it is time to upgrade, either to version 5 (the most recent release of which is 4.9.9.2), or to some other IDE such as Code Blocks. The compiler included by default in Dev-C++ 4 is the MinGW port of GCC 2.95, which is a rather ancient compiler.
    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

  9. #9
    Registered User
    Join Date
    May 2007
    Posts
    12
    My deadline is just a few days away,
    I'd rather not have to get used to a new compiler if I don't have too!

    Is it something I need to do to get the conversion to work?

    Thanks again
    L

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,634
    You could try:
    Code:
    Leigh = atoi(line.c_str());
    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

  11. #11
    Registered User
    Join Date
    May 2007
    Posts
    12
    Quote Originally Posted by laserlight View Post
    You could try:
    Code:
    Leigh = atoi(line.c_str());
    Goodness me.

    Laserlight - you are wonderful in every way possible!

    Thank you very much!

    I knew it would be something simple, but just didn't realise how simple! - Thank you again!

    (It works fine btw :-p )

    THANK YOU!

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,634
    You are welcome. I do hope you upgrade to a newer compiler for your next C++ project though
    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

  13. #13
    Registered User
    Join Date
    May 2007
    Posts
    12
    Quote Originally Posted by laserlight View Post
    You are welcome. I do hope you upgrade to a newer compiler for your next C++ project though
    I will I promise :-)

    Whilst your here, you couldn't tell me what the keyword is to prevent a variable from dying when a function ends could you?

    ie
    Code:
    void function()
    {
    DONTDIE int a =5;
    }
    
    void function2()
    {
    cout << a;
    }

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,634
    There is no such keyword (other than new, new[], and the making of the variable into a global or member variable, but I suspect those are not what you are looking for here).

    One option is to return a value from the function, and then call that function from the other function. You might want to read a tutorial on functions, say the one at cprogramming.com
    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

  15. #15
    Registered User
    Join Date
    May 2007
    Posts
    12
    what i want to do is create an integer array.

    but i don't know what size it will be until i have ran other functions within the class.

    All I want to do is create the integer array inside say function1 and have it available to other functions when function1 has finished...

    Basically my app reads a text file and discovers how many cd objects are in the text file, and then creates an int array of that size. Ie 4 cd's = int container[3]

    Then a different function re-reads the file to determine how many tracks there are for each cd and then puts them in.
    For example, if container[2] contained the value 6 - that would mean there were 6 tracks on cd 3.

    But for this to work , I need Container[] to be available to both functions. Can't create it at the beginning because I'll only know how big it should be after function 1 is ran...



    Alternatively maybe sort of declare the int array as a global at the top but not set the size of it until midway?
    possible? not?
    Last edited by LeighRogers; 05-09-2007 at 01:56 PM.

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

Similar Threads

  1. how to combine these working parts??
    By transgalactic2 in forum C Programming
    Replies: 0
    Last Post: 02-01-2009, 07:19 AM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. Debug Error Really Quick Question
    By GCNDoug in forum C Programming
    Replies: 1
    Last Post: 04-23-2007, 12:05 PM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-01-2003, 11:26 PM

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