-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMorseUI.cpp
More file actions
129 lines (110 loc) · 2.98 KB
/
Copy pathMorseUI.cpp
File metadata and controls
129 lines (110 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Alfred Ledgin
// 12/4/2015
// CS 303
// Project 3
#include "MorseSystem.h"
#include "MorseUI.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void MorseUI::execute()
{
quit = false; // Force the program to continue until the user quits.
do
{
displayMenu();
switch (menuOption)
{
case '1': obtainFile(); break;
case '2': decodeWord(); break;
case '3': encodeWord(); break;
case '4': quit = true; break;
default: break;
}
}
while (!quit);
}
void MorseUI::displayMenu()
{
cout << "Morse Code System" << endl << endl;
cout << "Enter a menu option:" << endl;
cout << "1. Enter a code file." << endl;
cout << "2. Decode a word." << endl;
cout << "3. Encode a word." << endl;
cout << "4. Quit." << endl << endl;
cin >> menuOption;
}
void MorseUI::obtainFile()
{
string filename;
ifstream file;
do
{
cout << "Enter the filename (or 'b' to go back):" << endl;
cin >> filename;
file.open(filename);
if (filename == "b") // Allow the user to go back.
break;
if (!file)
cout << "Error: File not found." << endl;
}
while (!file);
if (file)
codeSystem.setInput(file); // Define the Morse Code alphabet.
file.close();
}
void MorseUI::decodeWord()
{
string inputWord;
cout << "Enter a word in Morse Code." << endl;
handleInput(inputWord); // Handle spaces at cin prompt.
try
{
cout << "The word is: " << codeSystem.decodeWord(inputWord) << endl
<< endl; // Print the decoded word.
}
catch(std::exception& error)
{
cout << "Error: " << error.what() << endl << endl;
}
// Reference:
// "std::exception." _cplusplus.com_. cplusplus.com, 2015.
// Web. 3 Dec. 2015.
// <http://www.cplusplus.com/reference/exception/exception/>.
}
void MorseUI::encodeWord()
{
string inputWord;
cout << "Enter a word to encode:" << endl;
cin >> inputWord;
try
{
cout << "Morse Code: " << codeSystem.encodeWord(inputWord) << endl
<< endl; // Print the word in Morse Code.
}
catch(std::exception& error)
{
cout << "Error: " << error.what() << endl << endl;
}
// Reference:
// "std::exception." _cplusplus.com_. cplusplus.com, 2015.
// Web. 3 Dec. 2015.
// <http://www.cplusplus.com/reference/exception/exception/>.
}
void MorseUI::handleInput(string& inputString)
{
do
{
cin.ignore();
inputString = "";
getline(cin, inputString);
if (inputString == "")
inputString = "NULL";
}
while (inputString.length() > 500 || inputString.length() == 0);
// 500 chars allow 100 four-character Morse-coded letters plus 100 spaces.
}
// Source:
// Kuhail, Mohammad. "White spaces." Message to the author.
// 2 Dec. 2015. E-mail.