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
#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