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;
}
Comments
used code tags in first post :)

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

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).

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.

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.

This article has been dead for over six months. Start a new discussion instead.