Sign in to follow this  
TEUTON

[urgent]converting string to int

Recommended Posts

I am trying to take a string from the file and convert it into a number using this code
bool StringToInt(const string &s, unsigned long int &i)
{
  istringstream myStream(s);
  
  if (myStream>>i)
    return true;
  else
    return false;
}
But when the input is a very large string like 999999999999999999999 it fails Input string might be of 1000 digits. I tried taking unsigned long int but still it's very small...plz suggest someway to convert a very large number like this. I have Dev C++ latest version and VC6.0 at my end plz suggest some standard abiding code rather than some compiler specific datatype Thank you

Share this post


Link to post
Share on other sites
A one-thousand digit number? Good luck. You will probably have to write your own "Big Integer" class - it's something universities seem to set as coursework a bit, so maybe Google for it and find some acadamia code lying around.

Share this post


Link to post
Share on other sites
Quote:
Original post by _goat
A one-thousand digit number? Good luck. You will probably have to write your own "Big Integer" class - it's something universities seem to set as coursework a bit, so maybe Google for it and find some acadamia code lying around.


Basically I need to sum up the digits of the string...so I thought I will convert
it into a number and then follow simple divide and remainder method to calculate the sum of digits.

Can you suggest someother way to find the sum????

Share this post


Link to post
Share on other sites

DECLARE sum AS integer
DECLARE temp AS integer

FOR_EACH character_in_string
CONVERT character TO integer -> temp
ADD temp TO sum
REPEAT AS NECESSARY
So for "3497348", character zero would be '3', character one would be '4', etc.

Hope that helps.

Share this post


Link to post
Share on other sites
Try this - of course, you still have a problem if the sum of all the digits too big for an unsigned in to hold, but you'll need a nuch larger number to get there.

unsigned int value(char c)
{
switch(c)
{
case '0':
return 0;
case '1':
return 1;
case '2':
return 2;
case '3':
return 3;
case '4':
return 4;
case '5':
return 5;
case '6':
return 6;
case '7':
return 7;
case '8':
return 8;
case '9':
return 9;
default:
//throw something
}
}

unsigned int sum(const std::string& input)
{
unsigned int sum = 0;
for(std::string::const_iterator it = input.begin(),
endIt = input.end();
it != endIt;
++it)
{
sum += value(*it);
}

return sum;
}

Share this post


Link to post
Share on other sites
Another version (How I'd do it):

bool StringToInt(const string &s, unsigned long int &i)
{
i = 0;
for(size_t i=0; i<s.length(); ++i)
{
if((a[i] < '0') || (a[i] > '9'))
return false;
i += a[i]-'0';
}
return true;
}


Share this post


Link to post
Share on other sites
Nitage: Why the near-reimplementation of std::accumulate?

#include <functional>
#include <iostream>
#include <numeric>
#include <stdexcept>
#include <string>

template < typename Type >
struct sum_digits
:
public std::binary_function< Type const, std::string::value_type const, Type >
{

Type operator()(Type const & value, std::string::value_type const character)
{
if (character < '0' || character > '9')
{
throw std::out_of_range("sum_digits was passed a non-digit character");
}
return value + (character - '0');
}

};

int main()
{
try
{
std::string input1 = "1234567890";
std::string input2 = "666";
std::string input3 = "122112211221122112211221122112211221122112211221122112211221122112211221122112211221122112211221122112211221122112211221122112211221";
std::string input4 = "i18n";
std::cout << std::accumulate(input1.begin(), input1.end(), 0, sum_digits< unsigned int >()) << '\n';
std::cout << std::accumulate(input2.begin(), input2.end(), 0, sum_digits< unsigned int >()) << '\n';
std::cout << std::accumulate(input3.begin(), input3.end(), 0, sum_digits< unsigned int >()) << '\n';
std::cout << std::accumulate(input4.begin(), input4.end(), 0, sum_digits< unsigned int >()) << '\n';
}
catch (std::exception & exception)
{
std::cout << exception.what() << '\n';
}
}

Σnigma

Share this post


Link to post
Share on other sites
Obviously I modifed it a bit

bool StringToInt(const std::string &s, unsigned long int &i)
{
i = 0;
for(size_t j=0; j<s.length(); ++j)
{
if((s[j] < '0') || (s[j] > '9'))
return false;
i += s[j]-'0';
}
return true;
}




Edit:Where did the previous post go?

Share this post


Link to post
Share on other sites

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