Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

string to int c++

#include <iostream>
#include <string>

// namespace because why not
namespace dstd
{
    int StringtoInt(std::string str)
    {
        int multiplier = 1;
        int sum = 0;
        for(int i = str.size() - 1;i >= 0; i--)
        {
            switch(str[i])
            {
                case '0':
                sum += 0;
                break;
                case '1':
                sum += 1 * multiplier;
                break;
                case '2':
                sum += 2 * multiplier;
                break;
                case '3':
                sum += 3 * multiplier;
                break;
                case '4':
                sum += 4 * multiplier;
                break;
                case '5':
                sum += 5 * multiplier;
                break;
                case '6':
                sum += 6 * multiplier;
                break;
                case '7':
                sum += 7 * multiplier;
                break;
                case '8':
                sum += 8 * multiplier;
                break;
                case '9':
                sum += 9 * multiplier;
                break;
                case '-':
                sum *= -1;
                break;
                case ' ':
                continue;
                case 
            }
            multiplier *= 10;
        }

        return sum;
    }
}

int main()
{
  //just an example of how to use the function
    int a = dstd::StringtoInt("1992");
  	std::cout<<a<<'
';
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #string #int
ADD COMMENT
Topic
Name
5+2 =