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: 3672551 • 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

C program

main.c

#include "head.h"

int main(void)
{

   char type[3], src[15], destination[15];
   char morse_strings[91][6];
   char english_strings[91][6];

   FILE *infile = NULL;
   FILE *outfile = NULL;

   fill_morse(morse_strings);//populate the damn array
   welcome();//who needs a welcome screen nowadays, nobody rtfm

   system("cls");

   printf("c:/documents~/visual studio~/projects/morse code converter/:");
   scanf("%s %s %s", &type, &src, &destination); // scans your input into its appropiate string
                                               // didnt error check.. didnt need to i think?
   //infile = open_file(src);
   //outfile = dest_file(destination);
  
   infile = fopen(src, "r");// the above function calls didnt work for some reason..
   outfile = fopen(destination, "w"); //i would always get a bad pointer... got frustrated

   if ((infile == NULL) || (outfile == NULL))// if the files not there
   {
       printf("BAD FILES! ");//listen to the printf
       return 0;
   }
   else
   {
      
       if (strcmp(type, "-m") == 0)//if you type -m
       {
           english_scan(infile, outfile, morse_strings);// do this thing
       }
       else if (strcmp(type, "-t") == 0)//if you type -t
       {
           morse_scan(infile, outfile, morse_strings);//do this thing
       }
       else
       {
           printf("you did not choose the right type ");//if you didnt type the other two
       }                                                   //you must listen to teh printf
   }
   fclose(infile);//close
   fclose(outfile);//the files
   return 0;//is a re-turn another turn?
}

head.h

#ifndef HEADER_H
#define HEADER_H

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

void welcome(void);
FILE *open_file(char src[15]);
FILE *dest_file(char destination[15]);
void fill_morse(char morse[91][6]);
void english_scan(FILE* infile, FILE* outfile, char morse[91][6]);
void morse_scan(FILE* infile, FILE* outfile, char morse[91][6]);
#endif


func.c

#include "head.h"

void welcome(void)
{
   //This function is a welcome splash that gives instructions to the user
   char any = NULL;
   printf("Welcome to the text-morse morse-text converter! ");
   printf("-t for morse to english text ");
   printf("-m for english to morse ");
   printf("for eg: -t morse.txt english.txt");
   printf("Press enter to continue. . . ");//poop
   any = getchar();
}
FILE *open_file(char src[15])
{
   //This function was supposed to open the file and return it's pointer
   //however im having troubles making this function work...
   FILE *infile = NULL;
   infile = fopen(src, "r");
   return infile;
}
FILE *dest_file(char destination[15])
{
   //This function was supposed to open the file and return it's pointer
   //however im having troubles making this function work...
   FILE *outfile = NULL;
   outfile = fopen(destination, "r");
   return outfile;
}
void fill_morse(char morse[][6])
{
   //this was annoying... filling up the morse array
   //via strcpy to copy the morse value that corresponds
   //with its english conversion
   strcpy(morse['1'], ".----");
   strcpy(morse['2'], "..---");
   strcpy(morse['3'], "...--");
   strcpy(morse['4'], "....-");
   strcpy(morse['5'], ".....");
   strcpy(morse['6'], "-....");
   strcpy(morse['7'], "--...");
   strcpy(morse['8'], "---..");
   strcpy(morse['9'], "----.");
   strcpy(morse['0'], "-----");
   strcpy(morse['A'], ".-");
   strcpy(morse['B'], "-...");
   strcpy(morse['C'], "-.-.");
   strcpy(morse['D'], "-..");
   strcpy(morse['E'], ".");
   strcpy(morse['F'], "..-.");
   strcpy(morse['G'], "--.");
   strcpy(morse['H'], "....");
   strcpy(morse['I'], "..");
   strcpy(morse['J'], ".---");
   strcpy(morse['K'], "-.-");
   strcpy(morse['L'], ".-..");
   strcpy(morse['M'], "--");
   strcpy(morse['N'], "-.");
   strcpy(morse['O'], "---");
   strcpy(morse['P'], ".--.");
   strcpy(morse['Q'], "--.-");
   strcpy(morse['R'], ".-.");
   strcpy(morse['S'], "...");
   strcpy(morse['T'], "-");
   strcpy(morse['U'], "..-");
   strcpy(morse['V'], "...-");
   strcpy(morse['W'], ".--");
   strcpy(morse['X'], "-..-");
   strcpy(morse['Y'], "-.--");
   strcpy(morse['Z'], "--..");
}
void english_scan(FILE* infile, FILE* outfile, char morse[91][6])
{
   //this starts the english scan section
   int count = 0;   //charcter count to 0
   char temp = 0; //the temp character that gets scanned in
  
   do
   {
       fscanf(infile, " %c", &temp); //assign character to temp
       temp = toupper(temp); // uppercase the bitch

       if(!feof(infile)) //as long as its not at the end of file
       {
           fprintf(outfile, "%s ", morse[temp]);// print this!
           count++; //increase the character count
           //note: THIS COUNTER DOESNT WORK!? it didnt seem right?! ever.. such large numbers...
       }
   } while (!feof(infile));

   printf("DONE! English Characters Converted: %d ", &count);//print its done
}
void morse_scan(FILE* infile, FILE* outfile, char morse[91][6])
{
   char temp, tempstring[6];// the string the morse code gets stored in
   int i = 0, j = 0, k = 0; // i is making the string counter, j will help count characters, k helps sort
   //int found = 0; //found varible
   do
   {
       i = 0; //reset i
       //found = 0;//reset found
      
       do
       {
           temp = fgetc(infile); //get char
           if (temp != ' ') // as long as is it aint a space
           {
               tempstring[i] = temp;//the value of i = the scanned in character
               i++;
           }
           if (temp == ' ')
           {
               tempstring[i] = '';
           }
           if (temp == "10")
           {
               fprintf(outfile, " ");
           }
       }while ((temp != ' ') && (!feof(infile)));
  
                                                 //I was gonna try this, but couldnt get it
       /*for (k = 0; found != 1; k++)           //to work and im running outa time to figure this out
       {
           if((strcmp(morse[k], tempstring)) == 0)
           {
               fprintf(outfile, "%s ", morse[k]);
               found = 1;
           }
       }*/
       if (!strcmp(tempstring, ".----"))   //now begins long list of iffffff'ssss
       {                                   //couldnt get the above function to work
           fprintf(outfile,"1 ");           //worked on that algorithem for 2 hours
       }                                   //this list took me 5 minutes...
       if (!strcmp(tempstring, "..---"))   //but anyways, i just compare tempstring
       {
           fprintf(outfile,"2 ");           //.... to every possible morse code
       }
       if (!strcmp(tempstring, "...--"))
       {
           fprintf(outfile,"3 ");
       }
       if (!strcmp(tempstring, "....-"))
       {
           fprintf(outfile,"4 ");
       }
       if (!strcmp(tempstring, "....."))
       {
           fprintf(outfile,"5 ");
       }
       if (!strcmp(tempstring, "-...."))
       {
           fprintf(outfile,"6 ");
       }
       if (!strcmp(tempstring, "--..."))
       {
           fprintf(outfile,"7 ");
       }
       if (!strcmp(tempstring, "---.."))
       {
           fprintf(outfile,"8 ");
       }
       if (!strcmp(tempstring, "----."))
       {
           fprintf(outfile,"9 ");
       }
       if (!strcmp(tempstring, "-----"))
       {
           fprintf(outfile,"0 ");
       }
       if (!strcmp(tempstring, ".-"))
       {
           fprintf(outfile,"%c ",'A');
       }
       if (!strcmp(tempstring, "-..."))
       {
           fprintf(outfile,"%c ",'B');
       }
       if (!strcmp(tempstring, "-.-."))
       {
           fprintf(outfile,"%c ",'C');
       }
       if (!strcmp(tempstring, "-.."))
       {
           fprintf(outfile,"%c ",'D');
       }
       if (!strcmp(tempstring, "."))
       {
           fprintf(outfile,"%c ",'E');
       }
       if (!strcmp(tempstring, "..-."))
       {
           fprintf(outfile,"%c ",'F');
       }
       if (!strcmp(tempstring, "--."))
       {
           fprintf(outfile,"%c ",'G');
       }
       if (!strcmp(tempstring, "...."))
       {
           fprintf(outfile,"%c ",'H');
       }
       if (!strcmp(tempstring, ".."))
       {
           fprintf(outfile,"%c ",'I');
       }
       if (!strcmp(tempstring, ".---"))
       {
           fprintf(outfile,"%c ",'J');
       }
       if (!strcmp(tempstring, "-.-"))
       {
           fprintf(outfile,"%c ",'K');
       }
       if (!strcmp(tempstring, ".-.."))
       {
           fprintf(outfile,"%c ",'L');
       }
       if (!strcmp(tempstring, "--"))
       {
           fprintf(outfile,"%c ",'M');
       }
       if (!strcmp(tempstring, "-."))
       {
           fprintf(outfile,"%c ",'N');
       }
       if (!strcmp(tempstring, "---"))
       {
           fprintf(outfile,"%c ",'O');
       }
       if (!strcmp(tempstring, ".--."))
       {
           fprintf(outfile,"%c ",'P');
       }
       if (!strcmp(tempstring, "--.-"))
       {
           fprintf(outfile,"%c ",'Q');
       }
       if (!strcmp(tempstring, ".-."))
       {
           fprintf(outfile,"%c ",'R');
       }
       if (!strcmp(tempstring, "..."))
       {
           fprintf(outfile,"%c ",'S');
       }
       if (!strcmp(tempstring, "-"))
       {
           fprintf(outfile,"%c ",'T');
       }
       if (!strcmp(tempstring, "..-"))
       {
           fprintf(outfile,"%c ",'U');
       }
       if (!strcmp(tempstring, "...-"))
       {
           fprintf(outfile,"%c ",'V');
       }
       if (!strcmp(tempstring, ".--"))
       {
           fprintf(outfile,"%c ",'W');
       }
       if (!strcmp(tempstring, "-..-"))
       {
           fprintf(outfile,"%c ",'X');
       }
       if (!strcmp(tempstring, "-.--"))
       {
           fprintf(outfile,"%c ",'Y');
       }
       if (!strcmp(tempstring, "--.."))
       {
           fprintf(outfile,"%c ",'Z');
       }
       j++;
   } while (!feof(infile));
   printf("DONE! Number of morse code characters = %d", &j);//this counter dont work too!?
}


c++ program

#include <iostream>>

using namespace std;

char* getInput(int& length)
{
   length = 0;
   char* data = 0;
   char c;

   while ((c = cin.get()) != 10)
   {
       char* tmp = new char[++length];

       for (int i = 0; i < length - 1; i++)
           tmp[i] = data[i];

       tmp[length - 1] = c;
       delete data;
       data = tmp;
   }

   return data;
}

char* concat(char* beg, int begLength, char* end, int endLength)
{
   char* str = new char[begLength + endLength];

   for (int i = 0; i < begLength; i++)
       str[i] = beg[i];

   for (int i = 0; i < endLength; i++)
       str[i + begLength] = end[i];

   return str;
}

int getLength(char* data)
{
   int length = 0;

   while (data[length] != '')
       length++;

   return length + 1;
}

char* getSubstring(char* data, int beg, int end)
{
   char* str = new char[end - beg];

   for (int i = beg; i < end; i++)
       str[i - beg] = data[i];

   return str;
}

bool compare(char* a, int aLen, char* b, int bLen)
{
   if (aLen != bLen)
       return false;

   for (int i = 0; i < aLen; i++)
       if (a[i] != b[i])
           return false;

   return true;
}

char convertFromMorse(char** morseCode, char* morseVal, int morseLength, char* data, int length)
{
   for (int i = 0; i < morseLength; i++)
       if (compare(data, length, morseCode[i], getLength(morseCode[i]) - 1))
           return morseVal[i];

   return -1;
}

char* convertFromText(char** morseCode, char* morseVal, int morseLength, char data)
{
   for (int i = 0; i < morseLength; i++)
       if (morseVal[i] == data)
           return morseCode[i];

   return 0;
}

// ....   .   .-..   .-..   ---   .--   ---   .-.   .-..   -..
bool isValidMorseCode(char* morseCode, int length)
{
   if (morseCode[0] == ' ' || morseCode[length - 1] == ' ')
       return false;

   int spaceCount = 0;

   for (int i = 0; i < length; i++)
   {
       if (morseCode[i] != '-' && morseCode[i] != '.' && morseCode[i] != ' ')
           return false;

       if (morseCode[i] == ' ')
           spaceCount++;

       else if (spaceCount == 0 || spaceCount == 3 || spaceCount == 7)
           spaceCount = 0;

       else
           return false;
   }

   return true;
}

bool isValidMessage(char* msg, int length)
{
   for (int i = 0; i < length; i++)
       if (msg[i] != ' ' && (msg[i] < 'A' || msg[i] > 'Z'))
           return false;

   return true;
}

int getNumberOfLettersFromMorseCode(char* data, int length)
{
   int lettersCount = 0;

   for (int i = 0; i < length; i++)
   {
       if (data[i] == ' ')
       {
           if (data[i + 6] == ' ')
               i += 7;

           else
               i += 2;

           lettersCount++;
       }
   }

   return lettersCount + 1;
}

char* convertMorseString(char* data, int length, char** morseCode, char* morseVal, int morseLength, int& resultLength)
{
   char* letters = new char[getNumberOfLettersFromMorseCode(data, length)];
   resultLength = 0;
   int begI = 0;

   for (int i = 0; i < length; i++)
   {
       if (data[i] == ' ' && data[i + 2] == ' ')
       {
           letters[resultLength++] = convertFromMorse(morseCode, morseVal, morseLength, getSubstring(data, begI, i), i - begI);
           i += 3;
           begI = i;
       }

       else if (data[i] == ' ' && data[i + 6] == ' ')
       {
           letters[resultLength++] = ' ';
           i += 7;
           begI = i;
       }
   }

   letters[resultLength++] = convertFromMorse(morseCode, morseVal, morseLength, getSubstring(data, begI, length), length - begI);

   return letters;
}

char* convertTextString(char* data, int length, char** morseCode, char* morseVal, int morseLength, int& resultLength)
{
   resultLength = 0;
   char* result = 0;

   for (int i = 0; i < length; i++)
   {
       char* converted = convertFromText(morseCode, morseVal, morseLength, data[i]);
       int convertedLength = getLength(converted) - 1;

       result = concat(result, resultLength, converted, convertedLength);
       resultLength += convertedLength;

       if (i < length - 1)
       {
           result = concat(result, resultLength, "   ", 3);
           resultLength += 3;
       }
   }

   return result;
}
/*
   WARNING
   There are currently memory leaks every at the moment.
   I am too lazy to fix them right now
*/
int main()
{
   const int morseLength = 27;
  
   char* morseCode[morseLength] =
   {
       ".-",       // 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
       "       "
   };

   char morseVal[morseLength] =
   {
       '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',
       ' '
   };

   bool translateToMorse = false;

   while (true)
   {
       cout << "Do you want to translate text to Morse code (1) or translate Morse code to text (2)?";

       char input = cin.get();
       cin.ignore();

       if (input == '1')
       {
           translateToMorse = true;
           break;
       }

       else if (input == '2')
       {
           translateToMorse = false;
           break;
       }

       else
           cout << "Invalid input!" << endl;
   }

   while (true)
   {
       if (translateToMorse)
           cout << "Enter Text Message: ";

       else
           cout << "Enter Morse Code: ";

       int length = 0;
       char* input = getInput(length);

       for (int i = 0; i < length; i++)
       {
           cout << (i + 1) << ": " << input[i] << endl;
       }

       if (translateToMorse && !isValidMessage(input, length))
       {
           cout << "Invalid Text Message!" << endl;
           continue;
       }

       else if (!translateToMorse && !isValidMorseCode(input, length))
       {
           cout << "Invalid Morse Code!" << endl;
           continue;
       }

       int resultLength = 0;
       char* result;

       if (translateToMorse)
           result = convertTextString(input, length, morseCode, morseVal, morseLength, resultLength);

       else
           result = convertMorseString(input, length, morseCode, morseVal, morseLength, resultLength);

       for (int i = 0; i < resultLength; i++)
           cout << result[i];

       cout << endl;
       break;
   }

   cout << "Enter something to quit..." << endl;
   char x;
   cin >> x;

   return 0;
}