The program should invert the number, but for some reason this does not happen

#include <cmath> using namespace std; int main() { int mass[2]; int result = 0; int num; int i = 0; cin >> num; while (num > 0) { mass[i] = num % 10; i++; num = num / 10; } int f = 0; for (int g = sizeof mass - 1; g >= 0; g--) { result = (mass[f] * pow(10, g)) + result; f++; } cout << result << endl; system("pause"); return 0; } 

    1 answer 1

    Why don't you do it easier?

     #include <iostream> #include <cmath> using namespace std; int main() { int num, res = 0; cin >> num; while(num) { res = res*10 + num%10; num /= 10; } cout << res << endl; system("pause"); return 0; } 

    Whats up your code ...
    Do you work with double digits only? If not, you will have an exit abroad mas with all the ensuing consequences.
    Next, insert this output into the program:

     cout << sizeof mass << endl; 

    Apparently, the result will surprise you - it will not be 2, as you probably expected, but 2*sizeof(int) - and you will again get out of the array.

    Further, in fact, nothing is needed to comment :(

    • With all due respect, @Harry, you have written something wrong. I substitute num = 5, and at the output immediately 5. - BuilderC
    • And what should be? I understand you want to get the number written in reverse order, no? - Harry