Ok so I'm having some problems with this program I have to write as you might be able to tell it takes strings from the user either in english or morse and translates them to morse or english respectively. I got it working from English -> Morse for strings with no spaces but it terminates the concatonation after a space.
I appreciate whatever help you can give me. Written in C++ in the bloodshed compiler

//English -> morse code & vice versa 1 space per letter 3 spaces per word
#include<iostream>
#include<cstring>
using namespace std;

int main()
{           
      char Alpha[36] = {'a','b','c','d','e','f','g','h','i','j','k','l', 'm',
                                  'n','o','p','q','r','s','t','u','v','w','x','y','z',
                                  '1','2','3','4','5','6','7','8','9','0'};      
      std::string Morse[36] = {".-","-...","-.-.","-..",".","..-.","--.","....","..",
                               ".---","-.-",".-..","--","-.","---",".--.","--.-",".-.",
                               "...","-","..-","...-",".--","-..-","-.--","--..",".----",
                               "..---","...--","....-",".....","-....","--...","---..","----.","-----"};

      string mphrase;
      string ephrase;
      int choice = 0;
      
      cout<<"Please enter 1 for english to morse code translation or 2 for morse code to english translation. "<<endl;
      cin>>choice;
      if (choice == 1)
      {
      cout<<"Enter the phrase you would like to be encoded into morse."<<endl;
      cin>>ephrase;
      for (int i = 0; i < ephrase.length(); i++)
      {
          for (int h = 0; h < 36; h++)
          {
              if (ephrase[i] == ' ')
              {
              ephrase[i]++;
              }
                              
      else if ( ephrase[i] == Alpha[h] )
      {
            mphrase += Morse[h];
            mphrase += " ";
      }
      }  
      }
    cout<<mphrase<<endl;
      }
    /*  else if (choice == 2)
      {
      cin>>mphrase;
      
      ephrase += Morse[x]; 
      }
      else 
      {
      cout<<"Please enter a valid response. "<<endl;
      cin>>choice;
      }
      cout<<mphrase;
     */
            
system("pause");
return 0;
}
mrboolf commented: used code tags in first post :) +2

Recommended Answers

All 9 Replies

Is it a trim function you are looking for?? Deleting the spaces in front of and behind the string is it??

I can translate a string as long as it doesn't contain a space such as
abcdefg
but when I enter something like
I am cool
it only picks up the I

Sry man cn't figure it out, no the best at programming myself!!

Member Avatar for jencas

referring to the topic and omitting the necessary namespace and #includes:

string str("This is a test");
str.erase(remove_if(str.begin(), str.end(), bind2nd(equal_to<char>(), ' ')), str.end());

You should get ephrase with getline(cin, ephrase); instead of cin >> ephrase; .

Before doing that, though, you should flush the input stream (great thread on this by Narue) because of the previous cin >> choice; .

Also, your program would not translate "I am cool" as long as you don't convert to lowercase the I (same story for any uppercase letter).

Member Avatar for jencas
if (ephrase[i] == ' ')
              {
                  ephrase[i]++;
              }

Do you really really want to increment the value of the char at ephrase?

in regards to all the posts jencas first post didn't work if I recieved a string from the user it would work if it was already assigned in the program. In regards to the 2nd comment

if (ephrase[i] == ' ')
              {
              ephrase[i]=ephrase[i+1];
              }

I changed that up but still no results. Mr. I tried getline but I got nothing but I have not tried flushing the stream so I'm in the process of reading that now. I think I might be making this program more complicated than it seems though because I am supposed to be using the string tokenizer along with other string functions.

Member Avatar for jencas

Try:

for (int i = 0; i < ephrase.length(); i++)
      {
          if (ephrase[i] == ' ')
              continue;

          for (int h = 0; h < 36; h++)
          {
              if ( ephrase[i] == Alpha[h] )
              {
                   mphrase += Morse[h];
                   mphrase += " ";
               }
          }   
      }

Don't even try to shift part of the string, if you don't know how to do it

Can't you simplify Alpha, and Morse like this: string Alpha = "abcd...7890"; vector<string> Morse; You probably could come up with an algorithm to push_back() all the elements, for Morse. Or for now, do it manually for the first few ones, just to test it out.

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.