I have been trying to solve this problem but always encountered problems with trying to add the past value of a number with a new number to make it a new value.
Can anyone help?
Basically num1 should be equal to temp+ num1 everytime the loop runs.
1 2 3 4 5 6 7 8 9 10
for (int p=0; p<b; p++)
{
float h= b-p-1;
float i =pow(10, h);
temp= x[p] * i ;
num1= temp+num1;
}
You have a logical error in your source code that is the cause of the unexpected output. In the last three for loop you assign a variable to the result of an element of the character array 'x' times some other variable. The value returned by the subscript operator is that character's internal representation (which is a decimal integer), not its external representation (what is seen when that character is wrote to the standard output stream).
Subtracting the character literal '0' from the value returned by the subscript operator will give you the integer that the character represents.
so you're issue is more that none of the loops are being iterated more than once, rather than
trying to add the past value of a number with a new number to make it a new value
i think you stick a breakpoint on line 43, and check the values for p,b and h are what you expect.
we can see that if you use "2+2=4" or anything of that form, where the operator is always in the second position, it is obvious to see why you loops aren't working. Because: (int p=0; p<b; p++)