Sign in to follow this  

Little string2int conversion problem

This topic is 3933 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

int convStringToInt( std::string input ) // Only works for positive numbers
{
    int outcome = 0;
    int ciffer;
    
    for( int i = 0; i < input.length(); i++ ) {
        switch( input[input.length()-(i+1)] ) {
            case '0' :
                ciffer = 0;
                break;
            case '1' :
                ciffer = 1;
                break;
            case '2' :
                ciffer = 2;
                break;
            case '3' :
                ciffer = 3;
                break;
            case '4' :
                ciffer = 4;
                break;
            case '5' :
                ciffer = 5;
                break;
            case '6' :
                ciffer = 6;
                break;
            case '7' :
                ciffer = 7;
                break;
            case '8' :
                ciffer = 8;
                break;
            case '9' :
                ciffer = 9;
                break;
            default :
                return -1; // Error
        }
        outcome = outcome + ciffer*(10^i);
    }
    
    return outcome;
}

This is a little conversion function that is supposed to take a std::string and convert it to an int. Somehow, it doesn't work, it seems to give me wrong numbers back. A string "2" returns 20, a string "3" returns 30, but a string "32" returns 53, there just doesn't seem to be any logic behind it. I debugged it (on a piece of paper), and I can't seem to figure out what makes the function fail. Could someone please help me? Thanks in advance --Rob Spoel

Share this post


Link to post
Share on other sites
I haven't looked at the rest of it much, but:

outcome = outcome + ciffer*(10^i);
^ is a bitshift operator, it doesn't do exponents. #include <cmath> and use pow(10, i) instead.

Also, I'm assuming you're writing this function for an exercise/homework? There are better ways to do this (just to be sure :P )

EDIT: I'm slow :(

Share this post


Link to post
Share on other sites
Why would you make that? There are much better ways to do conversions.


#include <sstream>

template<class ReturnType, class Type>
ReturnType LexicalCast( Type Value )
{
ReturnType ReturnValue;
std::stringstream ss;
ss << Value;
ss >> ReturnValue;
return ReturnValue
}

Share this post


Link to post
Share on other sites
@ load_bitmap_file:
It's not homework, but you could see it as exercise :) I'm dusting off my programming skills a little, by writing small litle solutions for something like converting a std::string to an int. I followed your instruction, except using math.h, I remember using that a long time ago ;)

@ Scet:
I'm afraid that your code is far over my level of comprehension :(

Share this post


Link to post
Share on other sites
Quote:
Original post by c4c0d3m0n
@ Scet:
I'm afraid that your code is far over my level of comprehension :(


It's really quite simple. Let's look at the string to int case first:

int LexicalCast( std::string str ) {
int returnValue; // The resulting int
std::stringstream ss; // A stringstream. If you have ever used any of the C++ streams (such as fstream or cin/cout), then this is a stream that reads/writes from/to a string.
ss << str; // Put in our string
ss >> returnValue; // Get the resulting int
return returnValue; // return the int.
}




Now, with the use of templates, we can generalize this:

template< class ReturnType, class Type > 
ReturnType LexicalCast( Type value ) {
ReturnType returnValue;
std::stringstream ss;
ss << value;
ss >> returnValue;
return returnValue;
}

// Usage:

std::string str = "394025";
int value = LexicalCast< int >( str ); // convert string to int



Boost has an implementation of this.

Hope that helps.


jfl.

Share this post


Link to post
Share on other sites

This topic is 3933 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this