How to convert type string to char/double ?

This is a discussion on How to convert type string to char/double ? within the C++ Programming forums, part of the General Programming Boards category; in my program, i put the user enters into a string, then i need to get some information out of ...

  1. #1
    Never Exist Hermitsky's Avatar
    Join Date
    Jul 2004
    Posts
    149

    Question How to convert type string to char/double ?

    in my program, i put the user enters into a string, then i need to get some information out of the string and store it into a char or double. would anyone give me some help ? THX

    blow me ... ...

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    you need to specify what type of string you are using.

    For "simple" null terminated char arrays use atoi()
    if cString is a char[] and myInt is an int
    myInt = atoi(cString);

    For instances of the STL string class use the c_str() method in addition to atoi();

    myInt = atoi(stlString.c_str());

    Some other string classes have methods that facilitate the conversion

  3. #3
    Never Exist Hermitsky's Avatar
    Join Date
    Jul 2004
    Posts
    149
    i used this:
    Code:
    #include<string>
    string a;
    double b;
    char c;
    how can i pass value in these variable ?

    blow me ... ...

  4. #4
    Stinking it up. StinkyRyan's Avatar
    Join Date
    Jun 2004
    Posts
    61
    as he said up there by using c_str() function with the stl string class.

    Code:
    #include <iostream>
    #include <stdlib.h>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
      string a;
      int b;
      
      a = "123456789";
      
      b = atoi(a.c_str());
      
      /* b now equals 123456789 */
      
      cout<<b;
      
      system("PAUSE");	
      return 0;
    }
    Since atoi only takes input of older character arrays for strings you have to use the function c_str() to convert a stl string to one. That code puts "123456789" into string a and then using atoi() puts it as a variable into int b.
    Because I can't.

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,669
    And the C++ way...
    Code:
    #include <iostream>
    #include <sstream>
    #include <string>
    using namespace std;
    
    int main()
    {
        string a = "123.456 Z";
        double d;
        char c;
    
        istringstream istream(a);
    
        istream >> d >> c;
    
        cout << "d = " << d << endl;
        cout << "c = " << c << endl;
    
        return 0;
    }//main
    gg

  6. #6
    Never Exist Hermitsky's Avatar
    Join Date
    Jul 2004
    Posts
    149
    thank you for your help, now i have solved my problem.

    blow me ... ...

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  4. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 06:33 AM
  5. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM

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