Dom*_*Dom 0 c++ swap replace ifstream
我不知道为什么这不起作用,我需要交换两个字符作为a和b输入,它编译,但所有的字符被替换为输入为b的字符,任何建议?
while (n != exist)
{
cout<<"What is the letter you want to swap?"<<endl;
cin>>a;
cout<<"What is the letter you want to swap it with?"<<endl;
cin>>b;
if (inFile.is_open())
{
while (inFile.good())
{
inFile.get(c);
if( c = a )
{
outFile<< b;
}
else if (c = b)
{
outFile<< a;
}
else
{
outFile<< c;
}
}
}
else
{
cout<<"Please run the decrypt."<<endl;
}
cout<<"Another letter? <n> to stop swapping"<<endl;
cin>>n;
}
Run Code Online (Sandbox Code Playgroud)
if( c == a )
{
outFile<< b;
}
else if (c == b)
{
outFile<< a;
}
Run Code Online (Sandbox Code Playgroud)
=用于分配,==用于比较.
你拥有它的方式,只要a不是0(整数0,而不是字符'0'),第一个分支将始终执行.
if( c = a )并且else if (c = b)是可疑的.您分别将a的值和b的值分配给c.我相信如果赋值操作成功完成(也就是它),则块将执行.我相信你想要的是==操作员而不是=操作员.