Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting a string to an int 1

Status
Not open for further replies.

grande

Programmer
Feb 14, 2005
657
CA
I'm trying to check if a string is a number, then change it into a number if it's valid. No problem, it checks it, and it's all good.

BUT, how do I do the actual conversion? I've tried atoi(), but that's only for chars, and a couple other things.

Suggestions?

PS - If possible, also include how to change to a float.

-------------------------
Just call me Captain Awesome.
 
Depending on what technique you used to determine if it is a number, you can do the same to convert it. If you used stringstreams like my example in your earlier thread, you can easily modify that to return the converted value. Just return test instead of true.

Check out the C++ FAQ-LITE, especially 39.2 and 39.3. It might be too advanced for your needs, but the stringstream parts are exactly what you want.

You can also use atoi and atof for ints and floats, but like I said in the other thread, they aren't as nice as stringstreams in my opinion.
 
Here, ch-check out my code:
Code:
int CheckNumber(string NumIn)
{
    //This function will ensure that this is a valid integer.  If it is not
    //valid, the function will return -99.
    int NumOut, i=0;
    bool ItsStillGood=true;

    while ((ItsStillGood == true) && (i <= NumIn.length()))
    {
        ItsStillGood = isdigit(NumIn.at(i));
    }

    if (ItsStillGood == true)
    { *** NEED HELP HERE *** }
    else
    { NumOut = -99; }

    return NumOut;
}

That's what I have so far, so I'm not sure if your solution applies (I've never used stringstream before, and I'm already running behind so converting everything would be tedious).

-------------------------
Just call me Captain Awesome.
 
First, your version isn't exactly right, it should be (i < NumIn.length()) instead of <= or it will always throw an exception (the index at length() is out of bounds of the string). You also have to increment i.

Second, your version will not work with negative numbers.

Third, "*** NEED HELP HERE ***" should be:
Code:
NumOut = atoi(NumIn.c_str());

Fourth, here is that function using stringstreams:
Code:
int CheckNumber(const string& NumIn)
{
    istringstream strm(NumIn);
    int NumOut;
    char c;
    if (!(strm >> NumOut) || strm.get(c))
        return -99;

    return NumOut;
}
And last, whichever version you use, I would return a bool that says whether the conversion succeeded, or throw an exception, returning -99 is weird, but if you know that your number will never be -99 then I guess it is ok.

As for a float, it will be more difficult to change your version to work for a float, because you will have to check for the decimal point in addition to digits. You would also want to use atof instead of atoi. My stringstream version will work perfectly if you just change int to float in the code without any other changes.
 
Thanks alot. I had got the other mistakes before (< vs. <=, etc), but I Copy and Pasted from a backup (Don't really know why...).

Thanks for your help, here have a star!

-------------------------
Just call me Captain Awesome.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top