0

I'm fairly new to programming and I'm trying to get a function working that converts a string to an int. My idea with this function was to collect every number in the string and store it in another string, then convert it to an int.

The function returns the value 0.

What this function is supposed to do is return the converted number. Which should not be 0.

int getNumberFromString(int convertedNumber, string textToConvert)
{
    for (int i = 0; i < textToConvert.size(); i++)
    {
        string collectNumbers;
        int j = 0;
        if (textToConvert[i] == '1' || textToConvert[i] == '2' || textToConvert[i]   == '3' ||
            textToConvert[i] == '4' || textToConvert[i] == '5' || textToConvert[i] == '6' ||
            textToConvert[i] == '7' || textToConvert[i] == '8' || textToConvert[i] == '9' || textToConvert[i] == '0')
        {
            collectNumbers[j] = textToConvert[i];
            j++;
        }
        if (collectNumbers.size() == 0)
        {
            return false;
        }
        else if (collectNumbers.size() > 0)
        {

            stringstream convert(collectNumbers);
            if (!(convert >> convertedNumber))
            {
                convertedNumber = 0;
            }
            return convertedNumber;
        }
    }
}
8
  • Look up isdigit too! Commented Apr 21, 2014 at 22:26
  • IF the first character in the input string is not a number return false gets called during the first iteration of the for loop. FWIW There are a lot of other problems with this code. Commented Apr 21, 2014 at 22:27
  • Note in particular that you don't need the loop for a conversion via stringstream, and that the digits 0 through 9 are guaranteed consecutive values (this is not so for the English letters). Commented Apr 21, 2014 at 22:30
  • @JonathonReinhart operator>> is right here Commented Apr 21, 2014 at 22:30
  • @user2176127 Indeed. My bad - I blame this on C++ deciding to re-use an operator with a totally different meaning than C. Commented Apr 21, 2014 at 22:32

3 Answers 3

1

Maybe you should just use library function ?

int stoi (const string&  str, size_t* idx = 0, int base = 10);
Sign up to request clarification or add additional context in comments.

2 Comments

not all compilers in main use support this function yet.
Yep, but g++ do with option -std=c++11
1

You want somehting more like:

int getNumberFromString(int convertedNumber, string textToConvert) {
    int retval = 0;
    for (auto c: textToConvert) {
        retval *= 10;
        retval += c - '0';
    }
    return retval;
}

if you need to code it, or simply use stoi()

1 Comment

I would drop the get prefix. getSin(x), ouch.
1

Your MAIN problem is that you are trying to convert the number before you have collected all the digits. You should loop over all the digits (use isdigit or if (x >= '0' && x <= '9') to avoid long list of individual digits - or, if you really like to list all digits, use switch to make it more readable).

Once you have collected all the digits, then convert AFTER the loop.

The statement return false, will be the same as return 0; since false will get converted to an integer with the value zero. So you won't be able to tell the difference between reading the value zero from a string and returning false (this is not PHP or JavaScript where type information is included in return values).

1 Comment

I appreciate the help and it makes hell of a lot more sense now, cheers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.