Hi, I need to convert english to morse code, atm Im using iterator and store the morsecode in vector. and store the result on new vector the problems is it'doesn't do the line properly.
when I try to change the itr instead of store to new vector i't give me error because I try to replace single char with morse code which is a string. So does any1 can tell me how to do it properly???
thanks

Recommended Answers

All 3 Replies

Since you didn't post any code we can only guess that you are attempting to write back to the file that you read from. You can't do it that way. Open a different file for output then read a character from input file and write the morse code to the new output file.

BTW you don't need an interator. Depending on how you set up the vector you can index directly into the vector using the english character as the index value. Example: string code = array['A'];

Hi thanks for your reply, the problem is I not allowed to use arrays.

#include <iostream>
#include <vector>
#include <fstream>
#include <list>
#include <algorithm>


using namespace std;

vector<string> vectorresult;

class vectordata
{
   private:
    char english;
	string morse;
	
   public:
      vectordata() 
	  {
	  
      }
      
      vectordata(const vectordata &other) :
         english(other.english),
         morse(other.morse)
      {
         // copy constructor
      }
      
      void setvectordata(const char &e, string m) { 
         english = e;
         morse = m;
      }
      
      const char& getEnglish() const {
         return english;
      }
	  
	  const string& getMorse() const {
         return morse;
      }
      
      void printEmployee() const {
         cout << " " << morse << "  ";
      }
      
      void loadEmployee(ifstream &fin) {
         fin >> english;
         fin >> morse;
      }
      
};

void loadList2(list<char> &charList, const char *file)
{
   // load english text to be converted into link list

   ifstream fin;
   fin.open(file);
   
   if (!fin) {
      cout << "ERROR - unable to read " << file << "\n";
      exit(0);
   }

   char ch;
   while (!fin.eof()) {
      fin.get(ch);
      charList.push_back(ch);
   }
   fin.close();
}

void encode(list<char> &charList, vector<vectordata> &myvector)
{
   list<char>::iterator itr;
   vector<string>::iterator vitr;
   
   unsigned int k;
   char en;
   
   for (itr = charList.begin(); itr != charList.end(); itr++)
   {
	en = *itr;
	for (k=0; k<myvector.size(); k++)
	 {
		if(en==myvector[k].getEnglish())vectorresult.push_back(myvector[k].getMorse());;
		//*itr = myvector[k].getMorse();//doesn't work
	 }
   }
}
   
void printList(const list<char> &charList)
{
   // print the contents of the linked list
   
   list<char>::const_iterator itr;
   
   for (itr = charList.begin(); itr != charList.end(); itr++) {
      cout << *itr;
   }
   cout << "\n";
}

void loadList(vector<vectordata> &myvector)
{
   ifstream fin;
   vectordata vd;
  
      fin.open("morsecodes.txt");
	  while (!fin.eof())
	  {
      vd.loadEmployee(fin);
      myvector.push_back(vd);
	  }
   
   fin.close();
}

void printvectordata(const vector<string> &vectorresult, const char *save)
{
  
  ofstream myfile (save);
  if (myfile.is_open())
  {
  unsigned int k;
   for (k=0; k<vectorresult.size(); k++) {
   myfile << vectorresult[k]; } 
   myfile.close();
   }
   else cout << "Unable to open file";
}

int main(int argc, char *argv[])
{
   list<char> charList;  
   vector<vectordata> myvector;     

   if (argc != 4) {
      printf("Syntax : euro file\n");
      return 0;
   }
   
   cout << argv[1];
   if ( *argv[1] == 'e' )
   {
   loadList2(charList, argv[2]);
   //printList(charList);
   loadList(myvector);
   encode(charList, myvector);
   printvectordata(vectorresult, argv[3]);
   return 0;
   }
   else
   {
   cout << "error" << endl;
   return 0;
   }
}

My code it does work but when I try to execute ./a.out e somefile.txt output.txt the output.txt didn't have the correct alignment....
for example:
dog
cat monkey
result:
morsecode morsecode morsecode morsecode
something like that.
Does any1 know how to correct this allignment?

>>the problem is I not allowed to use arrays.
vector is an array.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.