943,882 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1047
  • C++ RSS
Sep 7th, 2009
0

crack a cipher with string arrays

Expand Post »
hey guys, I have the assignment to build a program to decode a cryptogram puzzle using provided word tables. for each table the program should print the decoded text generated by that table and the number of actual english words in the decoded text. the encoded text is in the file crypt.txt and its less than 80 characters. the codetables are in codetables.txt and each line contains a tablein the form of an ASCII string and the program should process however many tables there are. the file dict.txt contains the dictionary which the program should use to determine word validity.

I had the program set up to save each table then test all of them at once but decided to change it to read in a table then test it immediatly and output then move onto the next table. but on restructuring it I keep getting logic errors.

please take a look at my code and see what you think, also let me know if you have tips on how I could structure this better.
thanks, i'd appreciate all the help i can get
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3. #include <iomanip>
  4. #include <fstream>
  5. using namespace std;
  6.  
  7. //**********************************
  8. //*********Function Prototypes******
  9. //**********************************
  10. void buildAlpha (string&);
  11. void getCrypt (string&, string&);
  12. void getTable (string&, string&);
  13. void testTable (string&, string&, string);
  14. int findPositionOf (char, string);
  15. //**********************************
  16. //********Main Function*************
  17. //**********************************
  18.  
  19. int main(){
  20. // my arrays
  21. string crypt;
  22. string alpha;
  23. //string answer;
  24. string table, decrypt;
  25. buildAlpha (alpha);
  26. getCrypt (crypt, decrypt);
  27. getTable (table, decrypt);
  28.  
  29. cout << "Cipher" << endl;
  30. for(int i=0; i<80; i++)
  31. { cout << crypt [i]; }
  32. cout << " " << endl;
  33. cout << "Alphabet" << endl;
  34.  
  35. for(int i=0; i<26; i++)
  36. { cout << alpha [i]; }
  37. cout << " " << endl;
  38.  
  39. return 0;
  40. }
  41.  
  42. //**********************************
  43. //********Function Definitions******
  44. //**********************************
  45.  
  46. void buildAlpha (string & alpha)
  47. {
  48. for(int i=0; i<26; i++)
  49. { alpha [i] = 'a'+i; }
  50. }
  51.  
  52. void getCrypt (string & crypt, string & decrypt)
  53. {
  54. ifstream numdata;
  55. numdata.open("crypt.txt");
  56. if ( !numdata ) // Check to make sure file was opened
  57. {
  58. cout << "Error opening file" << endl;
  59. }
  60. else
  61. {
  62. int i = 0;
  63. {
  64. getline(numdata, crypt);
  65. i++;
  66. }
  67. for(int i=0; i<80; i++)
  68. { decrypt[i] = crypt [i]; }
  69. numdata.close();
  70. }
  71. }
  72.  
  73. void getTable (string & table, string & decrypt)
  74. {
  75. ifstream numdata;
  76. numdata.open("codetable.txt");
  77. if ( !numdata ) // Check to make sure file was opened
  78. {
  79. cout << "Error opening file" << endl;
  80. }
  81. else
  82. {
  83. while (!numdata.eof())
  84.  
  85. getline(numdata, table); //get table 1
  86. cout << "Table 1" << endl;
  87. for(int i=0; i<26; i++)
  88. { cout << table [i]; }
  89. cout << " " << endl;
  90.  
  91. testTable (table, decrypt, crypt); //test table 1
  92. cout << "Table 1 tested" << endl;
  93. for(int i=0; i<26; i++)
  94. { cout << decrypt [i]; }
  95. cout << " " << endl;
  96.  
  97. getline(numdata, table); //get table 2
  98. cout << "Table 2" << endl;
  99. for(int i=0; i<26; i++)
  100. { cout << table [i]; }
  101. cout << " " << endl;
  102.  
  103. testTable (table, decrypt, crypt); //table 2 tested
  104. cout << "Table 2 tested" << endl;
  105. for(int i=0; i<26; i++)
  106. { cout << decrypt [i]; }
  107. cout << " " << endl;
  108.  
  109.  
  110. getline(numdata, table);
  111. cout << "Table 3" << endl; //get table 3
  112. for(int i=0; i<26; i++)
  113. { cout << table [i]; }
  114. cout << " " << endl;
  115.  
  116.  
  117. testTable (table, decrypt, crypt); //table 3 tested against cipher
  118. cout << "Table 3 tested" << endl;
  119. for(int i=0; i<26; i++)
  120. { cout << decrypt [i]; }
  121. cout << " " << endl;
  122.  
  123. getline(numdata, table); //get table 4
  124. cout << "Table 4" << endl;
  125. for(int i=0; i<26; i++)
  126. { cout << table [i]; }
  127. cout << " " << endl;
  128.  
  129. testTable (table, decrypt, crypt); //table 4 tested against cipher
  130. cout << "Table 4 tested" << endl;
  131. for(int i=0; i<26; i++)
  132. { cout << decrypt [i]; }
  133. cout << " " << endl;
  134.  
  135. getline(numdata, table); //get table 5
  136. cout << "Table 5" << endl;
  137. for(int i=0; i<26; i++)
  138. { cout << table [i]; }
  139. cout << " " << endl;
  140.  
  141. testTable (table, decrypt, crypt); //test table 5
  142. cout << "Table 5 tested" << endl;
  143. for(int i=0; i<26; i++)
  144. { cout << decrypt [i]; }
  145. cout << " " << endl;
  146.  
  147. }
  148.  
  149. numdata.close();
  150. }
  151.  
  152. void testTable (string & table, string & decrypt, string crypt)
  153. {
  154. int letPos;
  155.  
  156. for (int i=0; i<=80; i++)
  157. {
  158. char letterToLookup = crypt[i];
  159. letPos = findPositionOf ( letterToLookup, table );
  160. decrypt[ i ] = table[ letPos ];
  161. }
  162. }
  163. int findPositionOf (char letterToLookup, string table)
  164. {
  165. for (int i=0; i<=26; i++)
  166. {
  167. if (letterToLookup == table[i])
  168. return i;
  169. }
  170. }
  171. }

this is the output that i get on compiling

sh-3.00$ g++ prog1.cpp
prog1.cpp: In function 'void getTable(std::string&, std::string&)':
prog1.cpp:91: error: conversion from 'char* (*)(const char*, const char*)' to non-scalar type 'std::string' requested
prog1.cpp:103: error: conversion from 'char* (*)(const char*, const char*)' to non-scalar type 'std::string' requested
prog1.cpp:117: error: conversion from 'char* (*)(const char*, const char*)' to non-scalar type 'std::string' requested
prog1.cpp:129: error: conversion from 'char* (*)(const char*, const char*)' to non-scalar type 'std::string' requested
prog1.cpp:141: error: conversion from 'char* (*)(const char*, const char*)' to non-scalar type 'std::string' requested
prog1.cpp: At global scope:
prog1.cpp:171: error: expected declaration before '}' token
prog1.cpp: In function 'int findPositionOf(char, std::string)':
prog1.cpp:170: warning: control reaches end of non-void function
Similar Threads
Reputation Points: 5
Solved Threads: 0
Light Poster
cbreeze is offline Offline
27 posts
since Dec 2008
Sep 7th, 2009
0

Re: crack a cipher with string arrays

You have brackets problems which will be impossible to solve until you format your code with consistent indentation. NetBeans and Visual Studio will do that for you. Then start counting brackets and make sure you don't have more ending brackets than starting brackets, or vice versa.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Sep 7th, 2009
0

Re: crack a cipher with string arrays

In getTable() , where do you declare crypt ?
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Sep 8th, 2009
-1

Re: crack a cipher with string arrays

thanks guys, I made some changes, got rid of decrypt and now just call get crypt in a loop to reset it, and a few other things. The codetables come in and output correctly but there seems to be a problem with the letter switch in test table and find position. because all the decrypted code comes out the same. Once again I'd appreciate some tips. thanks any tips help a bunch.heres the new improved code

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3. #include <iomanip>
  4. #include <fstream>
  5. using namespace std;
  6.  
  7. //**********************************
  8. //*********Function Prototypes******
  9. //**********************************
  10. void getCrypt (string&);
  11. void getTable (string&, string&);
  12. void testTable (string&, string&);
  13. int findPositionOf (char, string);
  14. //**********************************
  15. //********Main Function*************
  16. //**********************************
  17.  
  18. int main(){
  19. // my arrays
  20. string crypt;
  21. string table;
  22. getCrypt (crypt);
  23.  
  24. cout << "Cipher" << endl;
  25. for(int i=0; i<80; i++)
  26. { cout << crypt [i]; }
  27. cout << " " << endl;
  28.  
  29. getTable (table, crypt);
  30.  
  31. return 0;
  32. }
  33.  
  34. //**********************************
  35. //********Function Definitions******
  36. //**********************************
  37.  
  38. void getCrypt (string & crypt)
  39. {
  40. ifstream numdata;
  41. numdata.open("crypt.txt");
  42. if ( !numdata ) // Check to make sure file was opened
  43. {
  44. cout << "Error opening file" << endl;
  45. }
  46. else
  47. {
  48. getline(numdata, crypt);
  49. numdata.close();
  50. }
  51. }
  52.  
  53. void getTable (string & table, string & crypt)
  54. {
  55. int count = 1;
  56. ifstream numdata;
  57. numdata.open("codetable.txt");
  58. if ( !numdata ) // Check to make sure file was opened
  59. {
  60. cout << "Error opening file" << endl;
  61. }
  62. else
  63. {
  64. while (!numdata.eof())
  65. {
  66. getline(numdata, table); //get table 1
  67. cout << "Table " << count << endl;
  68. for(int i=0; i<26; i++)
  69. { cout << table [i]; }
  70. cout << " " << endl;
  71.  
  72. testTable (table, crypt); //test table 1
  73. cout << "Table "<<count<<" tested" << endl;
  74. for(int i=0; i<26; i++)
  75. { cout << crypt [i]; }
  76. cout << " " << endl;
  77.  
  78. getCrypt (crypt);
  79. count++;
  80. }
  81. }
  82.  
  83. numdata.close();
  84. }
  85.  
  86. void testTable (string & table, string & crypt)
  87. {
  88. int letPos;
  89.  
  90. for (int i=0; i<=80; i++)
  91. {
  92. char letterToLookup = crypt[i];
  93. letPos = findPositionOf ( letterToLookup, table );
  94. crypt[ i ] = table[ letPos ];
  95. }
  96. }
  97.  
  98. int findPositionOf (char letterToLookup, string table)
  99. {
  100. for (int i=0; i<=26; i++)
  101. {
  102. if (letterToLookup == table[i])
  103. return i;
  104. }
  105. }

I still get this error but it does compile

prog1.cpp: In function 'int findPositionOf(char, std::string)':
prog1.cpp:105: warning: control reaches end of non-void function
sh-3.00$ a.out

Heres my output, see the decrypted sections are the same so the problem must be in findposition I just don't see it.

Cipher
vshx su wgcy wx mcix sy, cvwclu gcu rxxe, cvwclu wsvv rx.;`
Table 1
bcdefghijklmnopqrstuvwxyza
Table 1 tested
vshxsuwgcywxmcixsyc
Table 2
bcsdfgleuvqwxzijkmhnptayor
Table 2 tested
vshxsuwgcywxmcixsyc
Table 3
lgtakcdevpjusfybxmhzrwoniq
Table 3 tested
vshxsuwgcywxmcixsyc
Table 4
cfelsvptmxzkdbyghinjauroqw
Table 4 tested
vshxsuwgcywxmcixsyc
Table 5
cirsdgavuwhjtxmynzopflbekq
Table 5 tested
vshxsuwgcywxmcixsyc
Table 6
irsdgavuwhjtxmynzopflbekq
Table 6 tested
skivideridirqiir
Reputation Points: 5
Solved Threads: 0
Light Poster
cbreeze is offline Offline
27 posts
since Dec 2008
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Sep 11th, 2009
0

Re: crack a cipher with string arrays

mmm, see this part at line 92
C++ Syntax (Toggle Plain Text)
  1. char letterToLookup = crypt[i];
  2. letPos = findPositionOf ( letterToLookup, table );
  3. crypt[ i ] = table[ letPos ];

shouldn't you use crypt[ i ] = alpha[ letPos ]; ??

This is what I think you trying to do
crypted[0]= !
So you look it up in the table using findPos and return an index
lets say:
table = $%! // so " ! " is the 3rd letter in an ALPHABET
so doing crypt[i] = table[letPos]; leaves crypt UNCHANGED

Instead

crypt[ i ] = alpha[ letPos ];

would change crypt[0] to the letter in the final alphabet
that is

alpha = abc
table = $%!

so '!' will correspond to letter c in the final alphabet

I actually edited your code an it ended like this its still a work in progress but I didnt want it to be irrecognizable to you

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3. #include <iomanip>
  4. #include <fstream>
  5.  
  6. using namespace std;
  7. /*
  8. decode a cryptogram puzzle using provided word tables.
  9.  
  10. for each table the program should print the decoded text generated by that table and the number of actual english words in the decoded text.
  11.  
  12. the encoded text is in the file crypt.txt and its less than 80 characters.
  13.  
  14. the codetables are in codetables.txt and each line contains a table in the form of an ASCII string and the program should process however many tables there are.
  15.  */
  16.  
  17. //**********************************
  18. //*********Function Prototypes******
  19. //**********************************
  20.  
  21.  
  22. void getCrypted (string & crypted );
  23. void getTable ( string );
  24.  
  25. void testTable ( string, string, int);
  26. int findPositionOf (char, string);
  27.  
  28. //**********************************
  29. //********Main Function*************
  30. //**********************************
  31.  
  32. string alpha = "abcdefghijklmnopqrstuvwxyz";
  33.  
  34. int main(){
  35.  
  36. string table, decrypt, crypt;
  37.  
  38. getCrypted(crypt);
  39. decrypt = crypt ;
  40.  
  41. //from here on just work with decrypt
  42.  
  43. cout << crypt<< "\n";
  44.  
  45. getTable (decrypt); //open tables and test 'em needs reading improvement
  46.  
  47. cout << "\n";
  48. system("pause");
  49.  
  50. }
  51.  
  52.  
  53. void getCrypted (string & crypt )
  54. {
  55. ifstream numdata;
  56. numdata.open("crypt.txt");
  57. if ( !numdata ) // Check to make sure file was opened
  58. cout << "Error opening file" << endl;
  59.  
  60. else
  61. {
  62.  
  63. getline(numdata, crypt);
  64.  
  65. numdata.close();
  66.  
  67. }
  68.  
  69. }
  70.  
  71. void getTable ( string decrypt)
  72. {
  73. ifstream numdata;
  74. numdata.open("codetable.txt");
  75. if ( !numdata ) // Check to make sure file was opened
  76. cout << "Error opening file" << endl;
  77. else
  78. {
  79. int c = 0;
  80. while (!numdata.eof()) //original
  81. {
  82. string table;
  83. getline(numdata, table); //get table 1
  84. cout << "\nTable#" << c << ":\n" << table << "\n";
  85.  
  86. testTable (table, decrypt, c);//this is the core of all the app
  87.  
  88.  
  89.  
  90. c++;
  91.  
  92. }
  93.  
  94.  
  95. numdata.close();
  96. }
  97.  
  98. }
  99. //here is the use of decrypt this cpy could be done in here
  100.  
  101. void testTable (string table, string decrypt, int wichTable)
  102. {
  103. int letterPos;
  104.  
  105. for (int i=0; i< decrypt.size(); i++)
  106. {
  107.  
  108. char letterToLookup = decrypt[i];
  109. letterPos = findPositionOf ( letterToLookup, table ); //this gets table to send it again? -what a waste we can accomodate this to be calc at use time
  110. decrypt[ i ] = alpha[ letterPos ]; //changed from table[i]
  111.  
  112. }
  113. cout << "\nTable#" << wichTable << "tested\n";
  114. cout << decrypt ;
  115. }
  116.  
  117. int findPositionOf (char letterToLookup, string table)
  118. {
  119. for (int i=0; i < table.size(); i++)
  120. {
  121. if (letterToLookup == table[i])
  122. return i;
  123. //note that if the crypted symbol is not in the table what the func returns
  124. // is undefined, making your program crash because of invalid memory access
  125.  
  126. }
  127. }

I tested with these files

C++ Syntax (Toggle Plain Text)
  1. // crypt.txt
  2. !@#$%^&*()

and

C++ Syntax (Toggle Plain Text)
  1. //codetable.txt
  2.  
  3. !@#$%^&*()_+|}{00000000000
  4. 00000000000!@#$%^&*()_+|}{

the Output was

C++ Syntax (Toggle Plain Text)
  1. !@#$%^&*()_+|}{
  2.  
  3. Table#0:
  4. !@#$%^&*()_+|}{00000000000
  5.  
  6. Table#0tested
  7. abcdefghijklmno
  8. Table#1:
  9. 00000000000!@#$%^&*()_+|}{
  10.  
  11. Table#1tested
  12. lmnopqrstuvwxyz
  13.  
  14. Press any key . .



as a final word you should really put effort in your code design
the first time you post it, it was very hard to see what was happening try not to be resigned with code that compiles you look foward to nice code too
Last edited by namehere05; Sep 11th, 2009 at 7:23 pm. Reason: I got the tags wrong
Reputation Points: 11
Solved Threads: 1
Light Poster
namehere05 is offline Offline
25 posts
since Dec 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: C++ Todo
Next Thread in C++ Forum Timeline: Question about typedef struct





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC