Tried making my addition for strings ... lots of struggle thus far
and dev c++ isn't really helping with its debugger needing some debugging of its own.
any ideas? the program always returns 1 (from the 2nd to last statement)
Code:
string addition(string num1,string num2)
{
string total = "";
int numSize;
int carry=0;
//I want the larger digit number on the top row, just as I would on paper.
// SWAP
if(num1.length() < num2.length())
{
string temp = num1;
num1 = num2;
num2 = temp;
}
numSize = num1.length();
//add zeros so they now match in length...so I can simply go from right to left adding cols.
while(numSize != num2.length())
{
num2= "0" + num2;
}
for(int i = numSize-1; i >= 0; --i)
{
int sum;
//Here I attempt to add characters together and resolve an integer
if(sum = (int)num2[i]+(int)num1[i]+carry > 9)
{
//when adding, the carry is at most 1.
carry = 1;
total=(convertInt(sum - 10))+total;
}
else
{
carry=0;
total = convertInt(sum) + total;
}
}
(carry == 1) ? total = "1" + total : 0;
return total;
}
(convertInt accepts an int and returns it as a string)