C++: String as Integer

Discussion in 'Computer Science & Culture' started by Rick, Aug 7, 2005.

Thread Status:
Not open for further replies.
  1. Rick Valued Senior Member

    Messages:
    3,336
    Hi,

    i was wondering as to how i could change a string to an int, or at least allow a string to behave as one.
    Say i have:
    Code:
    string balance ="100$";
    //now i want to add to this string another 100$ or probably take 50$ out
    
    how would i do it?

    Rick
     
  2. Google AdSense Guest Advertisement



    to hide all adverts.
  3. Nebula Occasionally Frequent Registered Senior Member

    Messages:
    906
    You need to perform what's called a type cast. I've done only minimal programming in C++ but I think you use the atoi() function:

    Code:
    int int_bal = atoi(balance);
    I think this function will only parse leading whitespaces, +/-, and then digits; you might encounter problems if your string contains another character, like the '$'.

    Keep in mind that integers can only represent whole numbers (duh!), which usually means they're lousy for handling currency. It will work if you're always dealing with whole dollars, or if you use the variable to represent the whole number of cents (and obtain the dollar value by /100, and the cents value by %100).

    -Kyle
     
  4. Google AdSense Guest Advertisement



    to hide all adverts.
  5. Rick Valued Senior Member

    Messages:
    3,336
    atoi works on strings? i havent tried...(will do that now)...

    ok Nebule you mean
    Code:
     
    reinterpret_cast (int)(string) ?
    
    ummm is that legal? ...i"ll give it a try though...
    (int) because it doesnt appear otherwise,on the post

    Please Register or Log in to view the hidden image!


    Thanks for input.
     
  6. Google AdSense Guest Advertisement



    to hide all adverts.
  7. okinrus Registered Senior Member

    Messages:
    2,669
    After checking the string is valid(you might want to check the behavior when an invalid string is given), and possibly taking out the $ character, you could do
    Code:
    #include <sstream>
    #include <string>
    ....
    std::stringstream ss;
    std::string numberStr = "140";
    int n = 0;
    
    ss << numberStr;
    ss >> n;
    
    atoi is different from a type cast, because atoi only takes c style strings. If you wanted to use atoi, call numberStr.c_str()
     
  8. Rick Valued Senior Member

    Messages:
    3,336
    but wouldnt that result in an overflow? since string size could be bigger than int n?
    its a nice idea though...
     
  9. okinrus Registered Senior Member

    Messages:
    2,669
    I'd have to look at a C++ manual to see exactly what would happen, but I'm pretty sure the stream state will just be left in some kind of failure state. You can then check it by either ss.fail() or ss.bad(). A different idea would be just to limit the number of characters in the string. You're pretty much safe to assume an int will have 16 bits; most machines have 32 bits. atoi has the same problem, so most C programmers recommend strtol.
     
Thread Status:
Not open for further replies.

Share This Page