- #1
Demon117
- 165
- 1
I am making a encoder/decoder program. I have made most of it but I am stuck on the last bit. I would like when i click decode the text it will read encode.txt from the same directory, read it and change it back to normal (undecode) and then show it on the screen.
Please can someone help?
Here is my code:
Please can someone help?
Here is my code:
Code:
#include <iostream>
#include <string>
#include <stdlib.h>
#include <fstream>
using namespace std;
const string cryptSymbols[]={"1","2","3","4","5","6","7","8","9","10","11","12",
"13","14","15","16","17","18","19","20","21","22","23","24","25","26","A","B","C",
"D","E","F","G","H","I","J","K","L","-"};
const string normalSymbols[]={"A","B","C","D","E","F","G","H","I","J","K","L","M",
"N","O","P","Q","R","S","T","U","V","W","X","Y","Z","1","2","3","4","5","6","7","8",
"9","0",".",","," "};
class crypt{
string inCode;
public:
crypt(string input);
string decode();
string encode();
};
crypt::crypt(string input){
inCode = input;
}
string crypt::decode(){
string output="";
string cryptLetter="";
char character;
char right;
int pointer=0;
while(pointer<inCode.length())
{character=inCode[pointer];
if(pointer<inCode.length()-1)
right=inCode[pointer+1];
if(!(character=' '))
cryptLetter+=character;
else{
for(int ctr=0;ctr<39;ctr++){
if(cryptLetter==cryptSymbols[ctr])
output+=normalSymbols[ctr];
}
cryptLetter="";
}
if((character==' ')&&(right==' ')){
output+=" ";
}
pointer++;
}
return output;
}
string crypt::encode(){
string output="";
string character="";
for(int ctr=0;ctr<inCode.length();ctr++){
character=inCode[ctr];
if(character==" ")
output+=" ";
for(int ctr2=0;ctr2<38;ctr2++){
if(character==normalSymbols[ctr2]){
output+=cryptSymbols[ctr2];
output+=" ";
}
}
}
return output;
}
int main()
{
string input;
string output;
char choice;
char yn;
char number;
yn='y';
while((yn!='n')&&(yn!='N')){
cout<<"Welcome to the Magic Decoder Ring!\n";
cout<<"Please choose an option:\n";
cout<<"1 Encode Text\n";
cout<<"2 Decode Text\n";
cin>>choice;
cin.clear();
cin.ignore();
if(choice=='1'){
ofstream file; //Declares file
file.open("encode.txt"); //Opens file
cout<<"Type the text you want encoded\n";
getline(cin,input);
crypt cryptCode(input+" ");
output=cryptCode.decode();
cout<<output<<'\n';
file<<cryptCode.encode()<<endl;
file.close();//Closes the file
cout<<"(Your code has been saved at encode.txt)\n";
cin.clear();
cin.ignore();
}
else if(choice=='2'){
ifstream infile("encode.txt");
crypt cryptCode(input+" ");
output=cryptCode.decode();
getline(cin,input);
cout<<output<<'\n';
cout<<"-Finished...";
cin.clear();
cin.ignore();
}
system("pause");
return EXIT_SUCCESS;
}}