For example, the first number, 178, comes out as 871.
No, it doesn't. You're processing digits from right to left and outputting them as they are processed. Why you think outputting digits as you process them is a good idea is anyone's guess. If you want to output the number, output the number. Strike line 28 and add cout << num; directly after line 17.
For the love of god, please use better variable names and pay attention to the warnings generated by your compiler.