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 decode the text it will oopen encode.txt, read it and change it back to normal (decode) the show it on the screen. If that cant be done could you do it so that it reads it, changes back to normal (decodes it) then saves it in the same encode.txt?

Heres the code:

/*
  Name: Encryptor/Decryptor
  Copyright: Just GFx & PSPhs
  Author: Mako-Infused
  Date: 26/10/08 16:44
  Description: 
*/

#include <iostream>
#include <string>
#include <stdlib.h>
#include <iostream>
#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 Mako-Infused Encoder/Decoder\n";
		cout<<"Please choose a 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 to be encoded\n";
			getline(cin,input);
			crypt cryptCode(input+" ");
			output=cryptCode.decode();
			cout<<output<<'\n';
			file<<cryptCode.encode()<<endl;
			file.close();//closes
			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();
		}

		
	return EXIT_SUCCESS;
}}

Recommended Answers

All 5 Replies

*bump* can you guys please help!

Maybe be a bit more specific as to what the problem is?

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?

This Bit:

}
		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();
		}

I would rethink the interface of the crypt class and specifically, the data members and constructors.

Why should crypt have a string data member? Shouldn't the crypt object take a string input, encrypt/decrypt as indicated, and close? If you agree, then remove inCode from the class declaration, create a default constructor, and pass the string to be encoded or decoded to the appropriate member function, returning the encoded or decoded string as desired.

class Crypt
{
   public:
     crypt();
     string encode(string);
     string decode(string);
};

int main()
{
   Crypt crypt;
   string input = //whatever;
   string encodedString = crypt.encode(input);
   cout << encodedString << '\n';
   string decodedString = crypt.decode(encodedString);
   cout << decodedString << '\n';
   if(input == decodedString)
      cout << "Yeah";
   else 
      cout << "Boo";
}

Can a mod please delete my script and close this thread? I have found a solution!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.