Hashtable to convert decrypt message

I'm trying to convert Morse code to the alphabet and numbers. So I have an encrypted message that I got from a file. I've created a hash table of morse code and alphabet. I created a function that will change one single piece of morse code at a time to the alphabet. What I'm having trouble with is decrypting a whole string of morse code. Specifically with my findMorse function I'm not sure which parameters I should be using on the binary that I converted to Morse code. Here's all of my code:
#include <iostream>
#include <bitset>
#include <vector>
#include <string>
#include <fstream>
#include<stdio.h>
#include<stdlib.h>
#include <stdio.h>
#include <string.h>
using namespace std;

class bitsetHashtable
{
private:
static const int tableSize = 60;
struct item
{
string morseCo;
string Alpha;
item* next;
};

item* HashTable[tableSize];

public:
bitsetHashtable();
int Hash(string key);
void AddItem(string morseCo, string Alpha);
int NumberOfItemsInIndex(int index);
void PrintTable();
void findMorse(string morseCo);
void convertToArray(string alpha);
};
bitsetHashtable::bitsetHashtable()
{
for (int i = 0; i < tableSize; i++)
{
HashTable[i] = new item;
HashTable[i]->morseCo = "empty";
HashTable[i]->Alpha = "empty";
HashTable[i]->next = NULL;
}
}
void bitsetHashtable::AddItem(string morseCo, string Alpha)
{
int index = Hash(morseCo);

if(HashTable[index]->morseCo=="empty")
{
HashTable[index]->morseCo = morseCo;
HashTable[index]->Alpha = Alpha;
}
else
{
item* Ptr = HashTable[index];
item* n = new item;
n->morseCo = morseCo;
n->Alpha = morseCo;
n->next = NULL;
while(Ptr->next !=NULL)
{
Ptr = Ptr->next;
}
Ptr->next = n;
}
}
int bitsetHashtable::NumberOfItemsInIndex(int index)
{
int count = 0;
if(HashTable[index]->morseCo == "empty")
{
return count;
}
else
{
count++;
item* Ptr = HashTable[index];
while (Ptr->next != NULL)
{
count++;
Ptr = Ptr->next;
}
}
return count;
}

void bitsetHashtable::PrintTable()
{
int number;
for (int i=0; i< tableSize; i++)
{
number = NumberOfItemsInIndex(i);
cout<<"--------------------\n";
cout<<"index = "<< i <<endl;
cout << HashTable[i]->morseCo<<endl;
cout<< HashTable[i]->Alpha<<endl;
cout<<"# of items = "<<number<<endl;
cout<<"--------------------\n";
}
}
int bitsetHashtable::Hash(string key)
{
int hash = 0;
int index;
index = key.length();

for(int i = 0; i<key.length();i++)
{
hash = hash + (int)key[i];
}
index = hash % tableSize;
return index;
}
void bitsetHashtable::findMorse(string morseCo)
{
int index = Hash(morseCo);
bool foundName = false;
string Alpha;
item* Ptr = HashTable[index];
while(Ptr != NULL)
{
if(Ptr->morseCo==morseCo)
{
foundName = true;
Alpha = Ptr->Alpha;
}
Ptr = Ptr->next;
}
if(foundName == true)
{

cout<<"Alphabet = "<<Alpha<<endl;
}
else
{
cout<<morseCo<<"'s info was not found in the Hash Table\n";
}
}

void bitsetHashtable::convertToArray(string Alpha)
{
int newAlpha = Alpha.length();
char char_array[newAlpha+1];
strcpy(char_array, Alpha.c_str());
for (int i=0; i<newAlpha; i++)
cout << char_array[i];
}
int main()
{
// cout<<"Hi";
bitsetHashtable Hashy;
string l = "";
Hashy.AddItem(".-","A");
Hashy.AddItem("-...","B");
Hashy.AddItem("-.-.","C");
Hashy.AddItem("-..","D");
Hashy.AddItem(".","E");
Hashy.AddItem("..-.","F");
Hashy.AddItem("--.","G");
Hashy.AddItem("....","H");
Hashy.AddItem("..","I");
Hashy.AddItem(".---","J");
Hashy.AddItem("-.-","K");
Hashy.AddItem(".-..","L");
Hashy.AddItem("--","L");
Hashy.AddItem("---","N");
Hashy.AddItem("---","O");
Hashy.AddItem(".--.","P");
Hashy.AddItem("--.-","Q");
Hashy.AddItem(".-.","R");
Hashy.AddItem("...","S");
Hashy.AddItem("-","T");
Hashy.AddItem("..-","U");
Hashy.AddItem("...-","V");
Hashy.AddItem(".--","W");
Hashy.AddItem("-..-","X");
Hashy.AddItem("-.--","Y");
Hashy.AddItem("--..","Z");
Hashy.AddItem("-----","0");
Hashy.AddItem(".----","1");
Hashy.AddItem("..---","2");
Hashy.AddItem("...--","3");
Hashy.AddItem("....-","4");
Hashy.AddItem(".....","5");
Hashy.AddItem("-....","6");
Hashy.AddItem("--...","7");
Hashy.AddItem("---..","8");
Hashy.AddItem("----.","9");
Hashy.AddItem(".--.-.","@");
Hashy.AddItem("---...",":");
Hashy.AddItem("--..--",",");
Hashy.AddItem("...-..-","$");
Hashy.AddItem( "-...-","=");
Hashy.AddItem("-.-.--","!");
Hashy.AddItem(".-.-.-",".");
Hashy.AddItem("..--..","?");
Hashy.AddItem(".----.","'");
// Hashy.PrintTable();
while(l != "exit")
{
cout<<"Search for ";
cin>> l;

if(l != "exit")
{
Hashy.findMorse(l);
}
}
Hashy.findMorse(l);
string letters = "";
fstream file;
bitsetHashtable hashObj;
file.open("//Users//vera//Desktop//Morse.bin", ios::in | ios::binary);
if (file)
{
cout<<endl;
cout<<"File open"<<endl;
while (file>>letters)
{
vector<bitset<8>> bset(letters.length());
for (int c = 0; c < letters.length(); c++)
bset[c] = letters[c];
for(auto b8 : bset)
{
for(size_t i = b8.size(); i > 0; i -= 2)
{
int x = b8[i - 1] * 2 + b8[i - 2];
if (x == 0) //Letter space
cout << ' ';
else if (x == 1) //Dash
cout << '-';
else if (x == 2) //Dot
cout << '.';
else if (x == 3) //Word space
cout << " ";
}
//string decrypted = "";
//decrypted += Hashy.findMorse(b8.to_string());
}
//Hashy.findMorse(letters);
}
}
else
{
cout<<"file not open"<<endl;
}
// index = hashObj.Hash(letters);
//// cout<<"index = " <<index<<endl;


file.close();
cout<<endl;
cout<<"File close"<<endl;
return 0;
}
What I'm having trouble with is decrypting a whole string of morse code

It depends on the input format. If you separate each morse token by a space you could read it into a string with getline and split it with a stringstream.
1
2
3
4
5
6
string input;
getline(cin, input);
istringstream iss(input);
string token;
while (iss >> token)
  // convert token into text 

Why don't you use a map<string, string> ?
It would make life soo much easier.
Topic archived. No new replies allowed.