Encoder

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2008
Posts: 4
Reputation: Mako-Infused is an unknown quantity at this point 
Solved Threads: 0
Mako-Infused Mako-Infused is offline Offline
Newbie Poster

Encoder

 
0
  #1
Oct 28th, 2008
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:
  1. /*
  2.   Name: Encryptor/Decryptor
  3.   Copyright: Just GFx & PSPhs
  4.   Author: Mako-Infused
  5.   Date: 26/10/08 16:44
  6.   Description:
  7. */
  8.  
  9. #include <iostream>
  10. #include <string>
  11. #include <stdlib.h>
  12. #include <iostream>
  13. #include <fstream>
  14.  
  15. using namespace std;
  16.  
  17. 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","-"};
  18. 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",".",","," "};
  19.  
  20. class crypt{
  21. string inCode;
  22. public:
  23. crypt(string input);
  24. string decode();
  25. string encode();
  26. };
  27.  
  28. crypt::crypt(string input){
  29. inCode=input;
  30. }
  31.  
  32. string crypt::decode(){
  33.  
  34. string output="";
  35. string cryptLetter="";
  36. char character;
  37. char right;
  38. int pointer=0;
  39.  
  40. while(pointer<inCode.length()){
  41. character=inCode[pointer];
  42.  
  43. if(pointer<inCode.length()-1)
  44. right=inCode[pointer+1];
  45.  
  46. if(!(character==' '))
  47. cryptLetter+=character;
  48.  
  49. else{
  50. for(int ctr=0;ctr<39;ctr++){
  51. if(cryptLetter==cryptSymbols[ctr])
  52. output+=normalSymbols[ctr];
  53. }
  54. cryptLetter="";
  55. }
  56. if((character==' ')&&(right==' ')){
  57. output+=" ";
  58. }
  59. pointer++;
  60. }
  61.  
  62. return output;
  63. }
  64.  
  65. string crypt::encode(){
  66. string output="";
  67. string character="";
  68. for(int ctr=0;ctr<inCode.length();ctr++){
  69. character=inCode[ctr];
  70. if(character==" ")
  71. output+=" ";
  72. for(int ctr2=0;ctr2<38;ctr2++){
  73. if(character==normalSymbols[ctr2]){
  74. output+=cryptSymbols[ctr2];
  75. output+=" ";
  76. }
  77. }
  78. }
  79.  
  80. return output;
  81. }
  82.  
  83. int main(){
  84.  
  85. string input;
  86. string output;
  87. char choice;
  88. char yn;
  89. char number;
  90. yn='y';
  91. while((yn!='n')&&(yn!='N')){
  92. cout<<"Welcome to Mako-Infused Encoder/Decoder\n";
  93. cout<<"Please choose a option:\n";
  94. cout<<"1 Encode Text\n";
  95. cout<<"2 Decode Text\n";
  96.  
  97. cin>>choice;
  98. cin.clear();
  99. cin.ignore();
  100.  
  101. if(choice=='1'){
  102. ofstream file; //declares file
  103. file.open("encode.txt"); //opens file
  104. cout<<"Type the Text you want to be encoded\n";
  105. getline(cin,input);
  106. crypt cryptCode(input+" ");
  107. output=cryptCode.decode();
  108. cout<<output<<'\n';
  109. file<<cryptCode.encode()<<endl;
  110. file.close();//closes
  111. cout<<"(Your code has been saved at encode.txt)\n";
  112. cin.clear();
  113. cin.ignore();
  114. }
  115. else if (choice=='2'){
  116. ifstream infile("encode.txt");
  117. crypt cryptCode(input+" ");
  118. output=cryptCode.decode();
  119. getline(cin,input);
  120. cout<<output<<'\n';
  121. cout<<"-Finished...";
  122. cin.clear();
  123. cin.ignore();
  124. }
  125.  
  126.  
  127. return EXIT_SUCCESS;
  128. }}
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 4
Reputation: Mako-Infused is an unknown quantity at this point 
Solved Threads: 0
Mako-Infused Mako-Infused is offline Offline
Newbie Poster

Re: Encoder

 
0
  #2
Oct 29th, 2008
*bump* can you guys please help!
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 390
Reputation: skatamatic will become famous soon enough skatamatic will become famous soon enough 
Solved Threads: 39
skatamatic skatamatic is offline Offline
Posting Whiz

Re: Encoder

 
0
  #3
Oct 29th, 2008
Maybe be a bit more specific as to what the problem is?
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 4
Reputation: Mako-Infused is an unknown quantity at this point 
Solved Threads: 0
Mako-Infused Mako-Infused is offline Offline
Newbie Poster

Re: Encoder

 
0
  #4
Oct 30th, 2008
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:
  1. }
  2. else if (choice=='2'){
  3. ifstream infile("encode.txt");
  4. crypt cryptCode(input+" ");
  5. output=cryptCode.decode();
  6. getline(cin,input);
  7. cout<<output<<'\n';
  8. cout<<"-Finished...";
  9. cin.clear();
  10. cin.ignore();
  11. }
Last edited by Mako-Infused; Oct 30th, 2008 at 1:13 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,758
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Encoder

 
0
  #5
Oct 30th, 2008
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.
  1. class Crypt
  2. {
  3. public:
  4. crypt();
  5. string encode(string);
  6. string decode(string);
  7. };
  8.  
  9. int main()
  10. {
  11. Crypt crypt;
  12. string input = //whatever;
  13. string encodedString = crypt.encode(input);
  14. cout << encodedString << '\n';
  15. string decodedString = crypt.decode(encodedString);
  16. cout << decodedString << '\n';
  17. if(input == decodedString)
  18. cout << "Yeah";
  19. else
  20. cout << "Boo";
  21. }
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 4
Reputation: Mako-Infused is an unknown quantity at this point 
Solved Threads: 0
Mako-Infused Mako-Infused is offline Offline
Newbie Poster

Re: Encoder

 
0
  #6
Nov 9th, 2008
Can a mod please delete my script and close this thread? I have found a solution!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 964 | Replies: 5
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC