Why the second version didn't work ?

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

The first version:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
  #include <iostream>
#include <cmath>
using namespace std;
int main () {
long long num,dev,devv;
char letter;
dev = 10;
devv = 1;
cin >> num;
cin >> letter;
while (num % dev != num ) {
        dev *= 10;
        devv *= 10;

}
while (dev != 10){
    cout << num % dev / devv << letter;
    dev /= 10;
    devv /= 10;
}
if (dev == 10){
    cout << num % dev;
}

return 0;
}


The second version :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <cmath>
using namespace std;
int main () {
int num,dev,devv,result,result2;
char letter;
dev = 10;
devv = 1;
cin >> num;
cin >> letter;
result = num % dev;
result2 = num % dev / devv;
while (result != num ) {
        dev *= 10;
        devv *= 10;

}
while (dev != 10){
    cout << result2 << letter;
    dev /= 10;
    devv /= 10;
}
if (dev == 10){
    cout << result;
}

return 0;
}
}


Last edited on
1
2
3
4
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.

Hope this helps,
Dave
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.
Last edited on
So the variable will not change unless i change my self ?
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.
Thank you very much ,You and Ganado ^_^
Last edited on
Topic archived. No new replies allowed.