In c Developing secret codes has interested people for centu
(In c++)
Developing secret codes has interested people for centuries. A simple coding scheme can be developed by replacing each character in a text file by another character that is a fixed number of positions away in the collating seuqence. For example if each character is replaced by the character that is two characters to the right in the alphabet then the letter a is replaced by the letter c, the letter b is replaces by the letter d and so on. Write a program that reads the text in a file and then generates a new file that contains the coded text using this scheme. Change only the alphanumeric characters.
this is what i have so far:
include<iostream>
#include<fstream>
using namespace std;
int main()
{
char c;
ifstream infile(\"d:\\\\firstcode.txt\");
if (infile.fail())
{
cerr << \"error opening firstcode.txt\";
exit(-1);
}
ofstream output;
output.open(\"d:\\\ ewcode.txt\");
if (output.fail())
{
cerr << \"error opening newcode.txt\";
exit(-1);
}
infile.get(c);
while (!infile.eof())
{
infile >> c;
for (int x=0; x > 0; ++x)
{
output << c;
++c;
}
output << c;
}
infile.close();
output.close();
return 0;
}
I keep getting incorrect variations of my input file (the word \'hello\') in my output file and I\'m at a complete loss for how to do this. I have a feeling my biggest issues are in my While loop if there is a way to just edit the while loop to make the rest of the code work as is i would be very appreciative, if not then if you could just add a few comment lines throughout the new code explaining it that\'d be a huge help!!
Solution
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char c,d;
ifstream infile(\"d:\\\\firstcode.txt\");
if (infile.fail())
{
cerr << \"error opening firstcode.txt\";
exit(-1);
}
ofstream output;
output.open(\"d:\\\ ewcode.txt\");
if (output.fail())
{
cerr << \"error opening newcode.txt\";
exit(-1);
}
//infile.get(c);
cout<<int(\'a\')<<endl;
char jk = \'c\';
cout<<int(jk)<<endl;
int tk = int(jk);
cout<<char(tk)<<endl;
while (!infile.eof())
{
infile >> c;
int t = int(c)+2;
d=c;
if(c>=\'a\' && c<=\'z\')
{
if(t>int(\'z\'))
{
t = t-26;
}
d = char(t);
}
else if(c>=\'A\' && c<=\'Z\')
{
if(t>int(\'Z\'))
{
t = t-26;
}
d = char(t);
}
else if(c>=\'0\' && c<=\'9\')
{
if(t>int(\'9\'))
{
t = t-10;
}
d = char(t);
}
output << d;
}
infile.close();
output.close();
return 0;
}

