First of all, I have to ask...why in the world are you using a
to store values between 0 and 9? int would have sufficed!
Secondly, you have to understand what
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 = 4, then a converting that to a char will give you a char of an EOT. But, money + 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)
for (int i=0;i<temp.length();i++)
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).