我对这个问题有疑问 我不知道我的代码应该如何真正工作,但我不知道为什么我得到错误的答案我不知道我的输入应该在文件结束时结束????你能帮我解决我的问题吗谢谢这里是问题的链接。 http://sharecode.ir/section/problemset/problem/2124 . 我的代码:
#include <iostream>
#include <string>
#include <cstdio>
using namespace std;
int main()
{
string alfa,alfa2,word;
cin>>alfa;
cin>>alfa2;
cout<<alfa2<<endl;
cout<<alfa<<endl;
cin.ignore();
while(getline(cin,word))
{
for(int i=0 ; i<word.size() ; i++)
{
bool cheak =false;
for(int j=0 ; j<alfa.size() ; j++)
{
if(word[i] == alfa[j])
{
printf("%c",alfa2[j]);
cheak = true;
break;
}
}
if(cheak == false)
printf("%c",word[i]);
}
printf("\n");
}
}
最佳答案
输入的前两行可能有空格
使用这个方法:
string alfa,alfa2,word;
getline(cin, alfa);
getline(cin, alfa2);
cout<<alfa2<<endl;
cout<<alfa<<endl;
然后将 cin.ignore()
放到你的代码中 :)
关于c++ - 替换密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20442902/