| | |
crack a cipher with string arrays
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Dec 2008
Posts: 23
Reputation:
Solved Threads: 0
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
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
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)
#include <iostream> #include <string> #include <iomanip> #include <fstream> using namespace std; //********************************** //*********Function Prototypes****** //********************************** void buildAlpha (string&); void getCrypt (string&, string&); void getTable (string&, string&); void testTable (string&, string&, string); int findPositionOf (char, string); //********************************** //********Main Function************* //********************************** int main(){ // my arrays string crypt; string alpha; //string answer; string table, decrypt; buildAlpha (alpha); getCrypt (crypt, decrypt); getTable (table, decrypt); cout << "Cipher" << endl; for(int i=0; i<80; i++) { cout << crypt [i]; } cout << " " << endl; cout << "Alphabet" << endl; for(int i=0; i<26; i++) { cout << alpha [i]; } cout << " " << endl; return 0; } //********************************** //********Function Definitions****** //********************************** void buildAlpha (string & alpha) { for(int i=0; i<26; i++) { alpha [i] = 'a'+i; } } void getCrypt (string & crypt, string & decrypt) { ifstream numdata; numdata.open("crypt.txt"); if ( !numdata ) // Check to make sure file was opened { cout << "Error opening file" << endl; } else { int i = 0; { getline(numdata, crypt); i++; } for(int i=0; i<80; i++) { decrypt[i] = crypt [i]; } numdata.close(); } } void getTable (string & table, string & decrypt) { ifstream numdata; numdata.open("codetable.txt"); if ( !numdata ) // Check to make sure file was opened { cout << "Error opening file" << endl; } else { while (!numdata.eof()) getline(numdata, table); //get table 1 cout << "Table 1" << endl; for(int i=0; i<26; i++) { cout << table [i]; } cout << " " << endl; testTable (table, decrypt, crypt); //test table 1 cout << "Table 1 tested" << endl; for(int i=0; i<26; i++) { cout << decrypt [i]; } cout << " " << endl; getline(numdata, table); //get table 2 cout << "Table 2" << endl; for(int i=0; i<26; i++) { cout << table [i]; } cout << " " << endl; testTable (table, decrypt, crypt); //table 2 tested cout << "Table 2 tested" << endl; for(int i=0; i<26; i++) { cout << decrypt [i]; } cout << " " << endl; getline(numdata, table); cout << "Table 3" << endl; //get table 3 for(int i=0; i<26; i++) { cout << table [i]; } cout << " " << endl; testTable (table, decrypt, crypt); //table 3 tested against cipher cout << "Table 3 tested" << endl; for(int i=0; i<26; i++) { cout << decrypt [i]; } cout << " " << endl; getline(numdata, table); //get table 4 cout << "Table 4" << endl; for(int i=0; i<26; i++) { cout << table [i]; } cout << " " << endl; testTable (table, decrypt, crypt); //table 4 tested against cipher cout << "Table 4 tested" << endl; for(int i=0; i<26; i++) { cout << decrypt [i]; } cout << " " << endl; getline(numdata, table); //get table 5 cout << "Table 5" << endl; for(int i=0; i<26; i++) { cout << table [i]; } cout << " " << endl; testTable (table, decrypt, crypt); //test table 5 cout << "Table 5 tested" << endl; for(int i=0; i<26; i++) { cout << decrypt [i]; } cout << " " << endl; } numdata.close(); } void testTable (string & table, string & decrypt, string crypt) { int letPos; for (int i=0; i<=80; i++) { char letterToLookup = crypt[i]; letPos = findPositionOf ( letterToLookup, table ); decrypt[ i ] = table[ letPos ]; } } int findPositionOf (char letterToLookup, string table) { for (int i=0; i<=26; i++) { if (letterToLookup == table[i]) return i; } } }
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
In
getTable() , where do you declare crypt ? "One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
•
•
Join Date: Dec 2008
Posts: 23
Reputation:
Solved Threads: 0
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
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
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> #include <iomanip> #include <fstream> using namespace std; //********************************** //*********Function Prototypes****** //********************************** void getCrypt (string&); void getTable (string&, string&); void testTable (string&, string&); int findPositionOf (char, string); //********************************** //********Main Function************* //********************************** int main(){ // my arrays string crypt; string table; getCrypt (crypt); cout << "Cipher" << endl; for(int i=0; i<80; i++) { cout << crypt [i]; } cout << " " << endl; getTable (table, crypt); return 0; } //********************************** //********Function Definitions****** //********************************** void getCrypt (string & crypt) { ifstream numdata; numdata.open("crypt.txt"); if ( !numdata ) // Check to make sure file was opened { cout << "Error opening file" << endl; } else { getline(numdata, crypt); numdata.close(); } } void getTable (string & table, string & crypt) { int count = 1; ifstream numdata; numdata.open("codetable.txt"); if ( !numdata ) // Check to make sure file was opened { cout << "Error opening file" << endl; } else { while (!numdata.eof()) { getline(numdata, table); //get table 1 cout << "Table " << count << endl; for(int i=0; i<26; i++) { cout << table [i]; } cout << " " << endl; testTable (table, crypt); //test table 1 cout << "Table "<<count<<" tested" << endl; for(int i=0; i<26; i++) { cout << crypt [i]; } cout << " " << endl; getCrypt (crypt); count++; } } numdata.close(); } void testTable (string & table, string & crypt) { int letPos; for (int i=0; i<=80; i++) { char letterToLookup = crypt[i]; letPos = findPositionOf ( letterToLookup, table ); crypt[ i ] = table[ letPos ]; } } int findPositionOf (char letterToLookup, string table) { for (int i=0; i<=26; i++) { if (letterToLookup == table[i]) return i; } }
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
•
•
Join Date: Dec 2008
Posts: 21
Reputation:
Solved Threads: 1
mmm, see this part at line 92
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
I tested with these files
and
the Output was
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
C++ Syntax (Toggle Plain Text)
char letterToLookup = crypt[i]; letPos = findPositionOf ( letterToLookup, table ); 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)
#include <iostream> #include <string> #include <iomanip> #include <fstream> using namespace std; /* 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 table in the form of an ASCII string and the program should process however many tables there are. */ //********************************** //*********Function Prototypes****** //********************************** void getCrypted (string & crypted ); void getTable ( string ); void testTable ( string, string, int); int findPositionOf (char, string); //********************************** //********Main Function************* //********************************** string alpha = "abcdefghijklmnopqrstuvwxyz"; int main(){ string table, decrypt, crypt; getCrypted(crypt); decrypt = crypt ; //from here on just work with decrypt cout << crypt<< "\n"; getTable (decrypt); //open tables and test 'em needs reading improvement cout << "\n"; system("pause"); } void getCrypted (string & crypt ) { ifstream numdata; numdata.open("crypt.txt"); if ( !numdata ) // Check to make sure file was opened cout << "Error opening file" << endl; else { getline(numdata, crypt); numdata.close(); } } void getTable ( string decrypt) { ifstream numdata; numdata.open("codetable.txt"); if ( !numdata ) // Check to make sure file was opened cout << "Error opening file" << endl; else { int c = 0; while (!numdata.eof()) //original { string table; getline(numdata, table); //get table 1 cout << "\nTable#" << c << ":\n" << table << "\n"; testTable (table, decrypt, c);//this is the core of all the app c++; } numdata.close(); } } //here is the use of decrypt this cpy could be done in here void testTable (string table, string decrypt, int wichTable) { int letterPos; for (int i=0; i< decrypt.size(); i++) { char letterToLookup = decrypt[i]; letterPos = findPositionOf ( letterToLookup, table ); //this gets table to send it again? -what a waste we can accomodate this to be calc at use time decrypt[ i ] = alpha[ letterPos ]; //changed from table[i] } cout << "\nTable#" << wichTable << "tested\n"; cout << decrypt ; } int findPositionOf (char letterToLookup, string table) { for (int i=0; i < table.size(); i++) { if (letterToLookup == table[i]) return i; //note that if the crypted symbol is not in the table what the func returns // is undefined, making your program crash because of invalid memory access } }
I tested with these files
C++ Syntax (Toggle Plain Text)
// crypt.txt !@#$%^&*()
and
C++ Syntax (Toggle Plain Text)
//codetable.txt !@#$%^&*()_+|}{00000000000 00000000000!@#$%^&*()_+|}{
the Output was
C++ Syntax (Toggle Plain Text)
!@#$%^&*()_+|}{ Table#0: !@#$%^&*()_+|}{00000000000 Table#0tested abcdefghijklmno Table#1: 00000000000!@#$%^&*()_+|}{ Table#1tested lmnopqrstuvwxyz 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
![]() |
Similar Threads
- getter/setter methods for String Arrays (Java)
- String arrays (C++)
- Defining string arrays outside of their class (C++)
- String Arrays (C++)
- string arrays: storing input (C++)
- Trying to figure out how to change a couple string Arrays to Boolean arrays.. (Java)
Other Threads in the C++ Forum
- Previous Thread: C++ Todo
- Next Thread: Question about typedef struct
Views: 555 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment based beginner binary c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multidimensional multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






