Code: Select all
#include "stdafx.h"
#include "stdafx.h"
#include <malloc.h>
#include <stdlib.h>
#include <string.h>
#include <fstream>
#include <iostream>
#include <iomanip>
void encode ( char ch ); //encode function
void enter(); // text entry function
void file3(); // read from a file function
void main_menu();
char *morses[] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};
int _tmain(int argc, _TCHAR* argv[]) {
main_menu();
}
void main_menu() {
//main menu
int opt;
printf("------------------------------------------------\n");
printf("-- Main Menu --\n");
printf("------------------------------------------------\n");
printf("-- Please Select An Option To Proceed --\n");
printf("-- 1. Convert keyboard entry to morse code --\n");
printf("-- 2. Convert a file to morse code --\n");
printf("------------------------------------------------\n\n");
printf("Enter an Option: ");
scanf_s("%d",&opt);
if ((opt!= 1) && (opt != 2) && (opt != 3)) {
printf("\nInvalid Option..... Try Again: "); //checks to see a valid option is entered
scanf_s("%d",&opt);
}
if (opt == 1) {
enter();
}
if (opt == 2) {
file3();
}
}
void enter() {
char *morses[] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};
char* word = (char*)malloc(100*sizeof(char));
char* result = (char*)malloc(300*sizeof(char));
char *resStart = result;
printf("Enter a word with capitals to convert\n");//me kefalaia
scanf("%s", word);
while(*word != '\0')
{
strcpy(result, morses[*word-'A']);
result += strlen(morses[*word-'A']);
word++;
}
printf("%s",resStart);
}
void file3() {
FILE *ipfileptr; //Import file pointer
FILE *opfileptr; //New file pointer
char* input = (char*)malloc(100*sizeof(char));
char* result3 = (char*)malloc(300*sizeof(char));
char *resStart3 = result3;
ipfileptr = fopen("text.txt" , "r");// Open the text document portfolio2
opfileptr = fopen("text2.txt" ,"w"); //Creates a second text document with name portfolio
while((*input= getc(ipfileptr))!=EOF){
strcpy(result3, morses[*input-'A']);
result3 += strlen(morses[*input-'A']);
input++;
putc(*resStart3, opfileptr);
}
}