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!

Recommended Answers

All 6 Replies

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 .

Dave,

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

Winbatch

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.

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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.