-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.cpp
62 lines (52 loc) · 1.25 KB
/
main.cpp
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
#include "Dictionary.h"
#include "Errors.h"
#include <fstream>
#include <iostream>
int main(int argc, char** argv) {
if(argc != 2) {
std::cerr << "USAGE: " << argv[0] << " [words-file]\n";
return 1;
}
Dictionary* dictionary = nullptr;
try {
std::ifstream file(argv[1]);
if(file.fail()) {
std::cerr << "Could not open file: " << argv[1] << '\n';
return 1;
}
dictionary = Dictionary::create(file);
}
catch(const std::exception& e) {
std::cerr << "Error reading words file: " << e.what() << '\n';
return 1;
}
while(true) {
std::string from;
std::string to;
std::cout << "From: ";
if(!std::getline(std::cin, from)) {
break;
}
std::cout << "To: ";
if(!std::getline(std::cin, to)) {
break;
}
try {
std::vector<std::string> chain = dictionary->hop(from, to);
for(const std::string& word: chain) {
std::cout << " - " << word << '\n';
}
}
catch(const NoChain& e) {
std::cout << "No chain.\n";
}
catch(const InvalidWord& e) {
std::cout << "Invalid word: " << e.what() << '\n';
}
catch(const std::exception& e) {
std::cerr << "ERROR: " << e.what() << '\n';
}
}
delete dictionary;
return 0;
}