how to convert a string to an int?

Discussion in 'Programming General' started by Cherry65, Nov 23, 2008.

how to convert a string to an int?
  1. Unread #1 - Nov 23, 2008 at 9:20 PM
  2. Cherry65
    Joined:
    Jan 31, 2007
    Posts:
    447
    Referrals:
    0
    Sythe Gold:
    0

    Cherry65 Forum Addict
    Banned

    how to convert a string to an int?

    well title says all. i have a string with numbers on it and want to convert it to an int so i can do calculations with the numbers.
     
  3. Unread #2 - Nov 23, 2008 at 11:49 PM
  4. Morphis
    Joined:
    Aug 2, 2008
    Posts:
    734
    Referrals:
    0
    Sythe Gold:
    0

    Morphis Apprentice
    Banned

    how to convert a string to an int?

    well I am not a c++ expert by no means, but I heard it is similar to java and in java you do
    Code:
    int "name your int" = Integer.parseInt("string"[1]);
     
  5. Unread #3 - Nov 24, 2008 at 8:23 AM
  6. Nullware
    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0

    Nullware Guru

    how to convert a string to an int?

  7. Unread #4 - Nov 24, 2008 at 10:18 AM
  8. Govind
    Joined:
    Apr 22, 2005
    Posts:
    7,825
    Referrals:
    13
    Sythe Gold:
    23
    Prove it! Trole Tier 1 Prizebox Tortoise Penis Le Monkey UWotM8? Wait, do you not have an Archer rank? Potamus

    Govind The One Musketeer
    Mudkips Highly Respected Retired Administrator

    how to convert a string to an int?

    char *buf = "123";
    int n = atoi(buf);
     
  9. Unread #5 - Nov 24, 2008 at 10:49 AM
  10. Cherry65
    Joined:
    Jan 31, 2007
    Posts:
    447
    Referrals:
    0
    Sythe Gold:
    0

    Cherry65 Forum Addict
    Banned

    how to convert a string to an int?

    im using the atoi function for this, but i have 1 problem. the compiler gives me an error that says:
    error C2664: 'atoi' : cannot convert parameter 1 from 'char' to 'const char *'
    Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
    and i tryed casting the var like this: atoi((const char *) var);
    that way the program compiles, but gets stuck, so that cast might have changed the var's data.
     
  11. Unread #6 - Nov 24, 2008 at 2:44 PM
  12. Govind
    Joined:
    Apr 22, 2005
    Posts:
    7,825
    Referrals:
    13
    Sythe Gold:
    23
    Prove it! Trole Tier 1 Prizebox Tortoise Penis Le Monkey UWotM8? Wait, do you not have an Archer rank? Potamus

    Govind The One Musketeer
    Mudkips Highly Respected Retired Administrator

    how to convert a string to an int?

    You're declaring it as a char when it should be char*.
     
  13. Unread #7 - Nov 24, 2008 at 6:22 PM
  14. Cherry65
    Joined:
    Jan 31, 2007
    Posts:
    447
    Referrals:
    0
    Sythe Gold:
    0

    Cherry65 Forum Addict
    Banned

    how to convert a string to an int?

    Ya well you are right. I solved it in another way:
    Code:
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    string a = "";
    cout << "Enter string to convert: ";
    cin >> a;
    int b = atoi(a.c_str());
    cout << a << " was converted into int: " << b << endl;
    system("PAUSE");
    } 
    
    now it works xD
    thank you all!
     
< silab client problem | C++ 6.0 >

Users viewing this thread
1 guest


 
 
Adblock breaks this site