Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Implementation (70 points) For this assignment, you will write a program that al

ID: 3672550 • Letter: I

Question

Implementation (70 points)

For this assignment, you will write a program that allows the user to enter a message by inputting morse code that you will translate to text OR text that you will translate to morse code. In the last assignment you learned how to use C++ strings-- you’re not allowed to use any C++ strings in this program. Instead, you must use C-style strings, which are stored in an array of characters, ended by a null character, ‘’.

What if the user doesn’t know how big the sentence or word is? Plus, it is awkward to limit the user to a specific number of characters or to ask the user for the number of characters he/she is going to enter. Don’t you want them to just enter something, and then an array will be created just big enough to store their sentence, like the C++ string?

You will create your C string by reading characters from the user until you see a newline,

‘ ’. Since you want to consume the newline character to know when to stop growing your array on the heap, then you will need to use the get() function, i.e. cin.get(). You can read more about this function and how to use it:

http://www.cplusplus.com/reference/istream/istream/get/

Remember, this function will no longer append the ‘’ to your string, it is up to you to make sure this is the last character in your c-style string!!!

The cstring library has a variety of functions that can help you work with C-style strings, but unfortunately you’re not allowed to use any of those, either. If you see a function you want to use in the cstring library, you need to figure out how to implement it yourself!

(Don’t worry-- the most useful ones aren’t complicated to recreate.)

Same rules as in Assignment #4, you must have function <= 15 lines of code (-10 pts, otherwise) and NO global variables (-10 pts, otherwise)!!!

Please refer to the following document for help with morse code:

http://morsecode.scphillips.com/morse2.html

Be creative with how you will do the translation from one to another using this chart!

Notice, you only have to worry about one case and only a few punctuations.

Morse code input and output rules:

· Three spaces between characters.

·Seven spaces between words.

Here is an example of your program:

Do you want to translate text to Morse code (1) or translate Morse code to text (2)? 1

Enter Text Message: Hello World

Morse Code:

.... . .-.. .-.. --- .-- --- .-. .-.. -..

Do you want to translate text to Morse code (1) or translate Morse code to text (2)? 2

Enter Morse Code:

.... . .-.. .-.. --- .-- --- .-. .-.. -..

Text Message: Hello World

(10 pts) Extra Credit Error Handling

Make it so that your program never errors on users input! Handle the following errors:

· The user enters a message or letter outside of A-Z, a-z, and some punctuation.

·The user enters morse code with something other than . and – in it.

· The user enters an invalid menu choice.

Explanation / Answer

#include <iostream>

#include <vector>

#include <stdio.h>

#include <string>

#include <cstring>

#include <iomanip>

using namespace std;

int main ()

{

    char morse[29] [7]={".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.","--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", ".--.-" , ".-.-", "---."};

    char text [29] [3]={ "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", "†", "„", "”"};

    int j, k;

    int borja=0, sok, count=0;

    unsigned int i;

    while (1)

    {

        int find_string = 0;

        int stop_string = 29;

        cout << "What do you want to do?"<<endl<< "(3) Translate to morse code"<<endl<< "(2) Translate from morse"<<endl<< "(1) Exit"<<endl;

        cin >> borja;

        if (borja==3)

        {

            string skrift;

            cout << "--------------------------" <<endl;

            cout<< "You have chosen to convert" << endl<< "From text to morse code," <<endl<< "What should i translate? : "<<endl;

            cin >> skrift;

            cout << endl << endl << skrift << " is in morse code : ";

            unsigned int str_lenght;

            str_lenght=skrift.size();

            cout << endl;

            for (i=0;i<str_lenght;i++)

            {

                find_string=0;

                while (find_string < stop_string)

                {

                    if (skrift[i]==text[find_string][0])

                    {

                        cout << morse[find_string] << " ";

                        break;

                   }

                    else

                        find_string = find_string+1;

                }

            }

        }

        else if (borja==2)

        {

            char skrift[50];

           cout << "--------------------------" <<endl;

          cout<< "You have chosen to convert" << endl<< "from morse code to text," <<endl<< "What should i translate? : "<<endl;

           skrift.getline(skrift, 50, endl) >> skrift;

           cout << endl << endl << skrift << "is in text : ";

           unsigned int str_lenght;

           str_lenght=50;

           cout << endl;

           for (i=0;i<str_lenght;i++)

           {

               find_string=0;

               while (find_string < stop_string)

               {

                   if (skrift[i]==morse[find_string][0])

                   {

                       cout << text[find_string];

                       break;

                   }

                   else

                   {

                       find_string = find_string+1;

                   }

               }

           }

        }

        else

            break;       

        cout << endl << endl << endl << endl;

    }

    cin.get();

    return 0;

}