字符交换在一个文件C++
问题描述:
我不知道为什么这不起作用,我需要交换两个字符输入为A和B,它编译,但所有的字符被替换为B输入的字符,任何意见?字符交换在一个文件C++
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;
}
答
在if
和else if
你需要使用==
,而不是=
。在C++/C中,您使用==
进行比较,=
进行分配。
答
if(c == a)
{
outFile<< b;
}
else if (c == b)
{
outFile<< a;
}
=
是分配,使用==
进行比较。
只要a不是0(整数0,不是字符'0'),就会始终执行第一个分支。
答
if(c = a)
和else if (c = b)
是可疑的。您正在将a的值分配给c,将b的值分配给c。我相信如果赋值操作成功完成(就是这样),该块将执行。我相信你想要==
运营商,而不是=
运营商。
谢谢你为什么我没有看到我没有想法,因为我想要它! – Dom 2011-12-13 18:34:16
我想你回答了第一个病人标记urs作为答案 – Dom 2011-12-13 18:37:17
@DominicBarrett:你可能想研究一下你的编译警告来指出这种错误。 – 2011-12-13 19:07:32