我有代碼可以工作,但只有一次。我需要輸入字符a
與輸入字符b
交換。通過循環第一次,它交換了兩個選定的字符,但在第二次和以後的迭代它什麼都不做,但保持outFile相同。我如何才能交換兩個以上的字符,直到我想停止?多次在一個文件中交換字符
ifstream inFile("decrypted.txt");
ofstream outFile("swapped.txt");
const char exist = 'n';
char n = '\0';
char a = 0;
char b = 0;
cout<<"\nDo u want to swap letters? press <n> to keep letters or any button to continue:\n"<<endl;
cin>>n;
while (n != exist)
{
cout<<"\nWhat is the letter you want to swap?\n"<<endl;
cin>>a;
cout<<"\nWhat is the letter you want to swap it with?\n"<<endl;
cin>>b;
if (inFile.is_open())
{
while (inFile.good())
{
inFile.get(c);
if(c == b)
{
outFile<< a;
}
else if (c == a)
{
outFile<< b;
}
else
{
outFile<< c;
}
}
}
else
{
cout<<"Please run the decrypt."<<endl;
}
cout<<"\nAnother letter? <n> to stop swapping\n"<<endl;
cin>>n;
}
我已添加inFile.seekg(0);和outFile.seekp(0);但仍然不是運氣 – Dom 2011-12-15 01:33:02
它可以是另一個標誌被提高除了eof?我不能看看是否會有或沒有 – Dom 2011-12-15 01:49:53