| | |
Encoder
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2008
Posts: 4
Reputation:
Solved Threads: 0
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:
Heres the code:
C++ Syntax (Toggle Plain Text)
/* 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; }}
•
•
Join Date: Oct 2008
Posts: 4
Reputation:
Solved Threads: 0
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:
Please can someone help?
This Bit:
•
•
•
•
C++ Syntax (Toggle Plain Text)
} 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(); }
Last edited by Mako-Infused; Oct 30th, 2008 at 1:13 pm.
•
•
Join Date: Jul 2005
Posts: 1,758
Reputation:
Solved Threads: 283
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.
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.
C++ Syntax (Toggle Plain Text)
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"; }
Klatu Barada Nikto
![]() |
Similar Threads
- Need Convolution Encoder C program (C)
- Encoder for PHP (PHP)
- Flash 8 - Video encoder (Graphics and Multimedia)
- Problems with Windows Script Encoder & #include directive (ASP)
Other Threads in the C++ Forum
- Previous Thread: intergrating ASM to C++
- Next Thread: Hash Table Implementation
Views: 964 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll dynamic encryption error file forms fstream function functions game givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linker linux loop looping loops map math matrix memory microsoft newbie news number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort stream string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






