[urgent]converting string to int

Started by
7 comments, last by TEUTON 18 years, 1 month ago
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
Advertisement
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.
[ search: google ][ programming: msdn | boost | opengl ][ languages: nihongo ]
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????
DECLARE sum AS integerDECLARE temp AS integer FOR_EACH character_in_string	CONVERT character TO integer -> temp	ADD temp TO sumREPEAT AS NECESSARY
So for "3497348", character zero would be '3', character one would be '4', etc.

Hope that helps.
[ search: google ][ programming: msdn | boost | opengl ][ languages: nihongo ]
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;}     
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 < '0') || (a > '9'))         return false;      i += a-'0';   }   return true;}

Thank you so much guys...I used the last version as suggested by evil steve and its working perfectly fine now.
Thanks again
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
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?

This topic is closed to new replies.

Advertisement