0
我编写了下面的代码将数字转换为字母。我的问题是程序将用户输入的第一个数字转换成正确的,但它给了其他人相同的第一个字母。使用堆栈将数字转换为字符
例如,如果用户输入的数目012
那么程序将其转化为ABC
不AAA
。
此外,我在system ("pause")
得到错误,我该如何解决它?
#include <iostream>
#include <stack>
using namespace std;
int main() {
int n;
string t,u="";
stack<string> s;
cout<<"Enter a number n: ";
cin>>n;
for(int i=0;i<n;i++){
cin>>t;
for(int j=0;j<t.length();j++){
if(t[i]=='0')
u= u+'A';
if(t[i]=='1')
u= u+'B';
if(t[i]=='2')
u= u+'C';
if(t[i]=='3')
u= u+'D';
if(t[i]=='4')
u= u+'E';
if(t[i]=='5')
u= u+'F';
if(t[i]=='6')
u= u+'G';
if(t[i]=='7')
u= u+'H';
if(t[i]=='8')
u= u+'I';
if(t[i]=='9')
u= u+'J';
}
s.push(u);
}
while(!s.empty()){
cout<<s.top()<<" ";
s.pop();
}
system("pause"); //error
return 0;
}
你应该缩进你的代码。你可以将10条if语句缩减为2行。 –
_系统还有错误(“暂停”)_:什么错误? –