c++ - 替换密码

标签 c++

我对这个问题有疑问 我不知道我的代码应该如何真正工作,但我不知道为什么我得到错误的答案我不知道我的输入应该在文件结束时结束????你能帮我解决我的问题吗谢谢这里是问题的链接。 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/

相关文章:

c++ - 在eclipse中隐藏控制台

c++ - 使用 Gdk::Pixbuf 创建的图像在显示时出现乱码

c++ - 单一虚拟继承

c++ - 在 C++ 中 ToString ("00.00000") 的等价物是什么?

c++ - Visual Studio 2012 更新 2 中的 std::async 衰减(丢失)右值引用。任何解决方法?

c++ - 如何就地构建可选聚合?

c++ - LoadLibrary 与将二进制 dll 加载到进程中?

c++ - Visual Studio 不允许省略默认参数?

c++ - 使用哪个 : move assignment operator vs copy assignment operator

c++ - 为什么 std::deque 子数组大小是固定的?