I made a program that put letter between numbers which you enter ...
I made two version of this code but the second code didn't work although that I just made one more variable and put it instead of (num % dev) as the following code
while (num % dev != num ) {
dev *= 10;
devv *= 10;
}
Since dev changes each time through the loop, the value of num%dev changes also.
1 2 3 4 5 6
result = num % dev;
result2 = num % dev / devv;
while (result != num ) {
dev *= 10;
devv *= 10;
}
Here the value of result is set before entering the loop. So even though dev changes each time through the loop, result does not.
You may be thinking that C++ variables are like cells in a spreadsheet. In a spreadsheet you can assign a formula to a cell and when anything in the formula changes, the value of the cell with the formula changes too. C++ isn't like that. Variables contain values that are set when you assign something to them. The value remains unchanged until the program changes the variable itself.
To be honest i didn't understand you well in the last to lines you wrote ! and what should i do to make "result" variable changes ?
And thank you anyways ^_^
and what should i do to make "result" variable changes ?
Assign an up-to-date value for it, before you print it on line 24.
The only time you currently assign to result is on line 11, result = num % dev;
result will still have the value assigned to it on line 11 when you print it on line 24, unless you assign something else to it.
Unlike a math variable, a C++ int variable cannot be "symbolic". It can only hold a number, like 432. It can't contain an entire math expression.
So the variable will not change unless i change my self ?
Exactly. When the program executes a statement like result = num % dev, it evaluates the expression on the right side using the values of the variables (num and dev) as they exist at that time. The result of the evaluation is a number. This number gets stored in the variable result. The number remains unchanged until you assign a new value to result.
So even if num and dev change, the value of result remains the same.