0

if i try to use this constructor
BigNum d("-2345");

how do i get the compiler to convert the string to a negative number?here's the code i have for this constructor

BigNum::BigNum(string s)
{
	num = new node;	
	num -> next = NULL;
	num -> val = 0;
	int i, j, r;
	i = s.size();
	i--;
	r = 1;
	node * curr;
	curr = num;
	sign = "+";
	
	while (i >= 0)
	{
		j = s[i] - 48;
		
		if (r == 1)
			curr -> val = j;
		
		if (r == 2)
			curr -> val += j*10;

		if (r == 3)
		{
			curr -> val += j*100;
			if(i > 0)
			{
				curr -> next = new node;
				curr = curr -> next;
				curr -> next = NULL;
			}
			r = 0;
		}

		r++;
		i--;
	}


}

<< moderator edit: added [co[u][/u]de][/co[u][/u]de] tags >>

all help is greatly appreciated!

3
Contributors
6
Replies
7
Views
11 Years
Discussion Span
Last Post by LordJayno
0

A guess would be something like this:

sign = s[0] == '-' ? '-' : '+';

For me to give better answers, I'd really have to know a lot more about BigNum .

0

Couldn't you just use istringstream and ostringstream to convert it?

If the value being converted were within the range of the integral type being used, yes. But I'm assuming that BigNum is intended to be one of those thingys that can hold, say, a 100-digit number.

0

i ended up checking s[0] and then if it contained '-' i changed the sign of the bignum and changed s[0] to 0. thx for your help guys!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.