我已经用C ++编写,提示用户输入一个小程序,用户给出了一个数字,然后电脑显示的数目逆转。
I've written a small program in C++ that prompts the user for input, the user gives a number, then the computer displays the number reversed.
例如:17变为71。123变为321
For example: 17 becomes 71. 123 becomes 321.
这是程序:
#include <iostream>
#include <string> //for later use.
using namespace std;
int rev(int x)
{
int r = 0;
while(x)
{
r = (r*10) + (x%10);
x = x/10;
}
return r;
}
int main()
{
int nr;
cout << "Give a number: ";
cin >> nr;
rev(nr);
cout << nr;
return 0;
}
该方案的最终结果:打印相同的号码,函数没有影响。我究竟做错了什么?我尝试了几种解决方案,但都无济于事。
The final result of the program: prints the same number, function has no effect. What am I doing wrong? I tried several solutions but to no avail.
您需要修改转(NR);
到 NR =转( NR);
或你的函数交替更改为:
or alternately change your function to:
void rev(int& x)
{
int r = 0;
while(x)
{
r = (r*10) + (x%10);
x = x/10;
}
x = r;
}