So I have to make a program where I calculate the sum of some elements in an array and then find the number of digits of the sum (e.g. if the sum of several elements is 175, I have to print the value of the sum (175) and the number of digits of 175 (3)).
To determine the number of digits I use the following "while" loop:
while (sum > 0)
{
sum /= 10;
digits++;
}
As you might have noticed, at the end of the loop the sum is 0. So I thought of creating an alias of "sum":
int& rSum = sum;
So I simply substituted "sum" with "rSum" in the array, in order to find the numbers of digits, and printed sum in the end of the program. Anyways, the value of "sum" after the loop is 0, in other words equal to "rSum". So I guess, when you create an alias to a certain variable, modifying the alias, modifies the variable itself, which is a problem in my case.
My question is, can I create the program with aliases (or using references, pointers etc.), or is the only way by creating a copy of the "sum" variable (int rSum = sum;
)?