First of all, I have to ask...why in the world are you using a
long double
to store values between 0 and 9? int would have sufficed!
Secondly, you have to understand what
amnt=(amnt[i]*10)+money[i]
is actually doing in the code.
A string is really just an array of char's, so you have to start there. Understand that a char representation of an integer uses an ascii table. So, if money[0] = 4, then a converting that to a char will give you a char of an EOT. But, money[0] + 48 converted to a char gives a char of 4 because the ascii value for 0 is 48.
The other thing to realize is that a string plus a string just concatenates is. So, if amnt = "45" and you add "8", it will become "458", not "57".
So, the way that you've done it, you would really want
amnt = amnt + (money[i] + 48)
But, what if the person only wants to enter 45 as their number? With your solution, they would have to enter 00000045 to get the program to run.
Hans is correct in that you should take a single integer as an input.
Then, you could modify your code to look like:
string convertInt(int number)
{
if (number == 0)
return "0";
string temp="";
string returnvalue="";
while (number>0)
{
temp+=number%10+48;
number/=10;
}
for (int i=0;i<temp.length();i++)
returnvalue+=temp[temp.length()-i-1];
return returnvalue;
}
or you could just use a stringstream (though if this is for a class check with your professor if you can use objects you haven't learned yet).