设计一个c++程序,输入一个4位数,将其反序输出.例如1234,输出4321.

#include<iostream.h>
void main()
{
int a,b,c,d,n;
for(n=1000;n<10000;n++)
cin>>n;
a=n/1000;
b=n%1000/100;
c=n%1000%100/10;
d=n%10;
cout<<d<<c<<b<<a<<endl;
}
我的这个程序错在哪啊,为什么总是输出0009啊,我在线等啊,有会的,希望告诉我啊!

#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << "请输入4位数:";
string instr;
cin >> instr;
string outstr;
for (int i=0; i<4; ++i)
{
outstr += instr[4-i-1];
}
cout << "反序输出:" << outstr << endl;
}
附:
删掉你的"for(n=1000;n<10000;n++)"那行
否则输入要循环9000次追问

你的程序是对的,但是我想知道我的程序在哪里,那样为什么不行啊?

温馨提示:答案为网友推荐,仅供参考
第1个回答  2020-06-07
#include
<iostream>
#include
<string>
using
namespace
std;
int
main()
{
cout
<<
"请输入4位数:";
string
instr;
cin
>>
instr;
string
outstr;
for
(int
i=0;
i<4;
++i)
{
outstr
+=
instr[4-i-1];
}
cout
<<
"反序输出:"
<<
outstr
<<
endl;
}
附:
删掉你的"for(n=1000;n<10000;n++)"那行
否则输入要循环9000次