a gleam implementation of a CS assignment originally written in cpp

feat: implement main search algorithm and failure states

Changed files
+93 -10
src
+1 -1
makefile
···
cd test && ./test.sh
exec:
-
./build/lab66
run: lab66 exec
···
cd test && ./test.sh
exec:
+
./build/lab66 test/OT.txt
run: lab66 exec
+92 -9
src/lab66.cpp
···
int main(int argc, char **argv) {
ifstream OT;
-
string filename = "../test/OT.txt";
string book;
int chapter;
int verse;
// get path to OT.txt if provided otherwise use default path
-
if (argc == 2) {
-
filename = argv[1];
-
}
-
OT.open(filename);
if (!OT.is_open()) {
-
cout << filename << ": failed to open file";
return 1;
}
···
cout << "the book: ";
getline(cin, book);
-
cout << endl;
cout << "the chapter: ";
cin >> chapter;
-
cout << endl;
cout << "the verse: ";
cin >> verse;
cout << endl;
-
cout << book << " " << chapter << ":" << verse;
OT.close();
···
int main(int argc, char **argv) {
ifstream OT;
+
ofstream OF;
+
string inputFilename = "../test/OT.txt";
+
string outputFilename = "build/verses.txt";
+
string book;
+
string normalizedBook;
int chapter;
int verse;
// get path to OT.txt if provided otherwise use default path
+
// and then do the same for the verses.txt
+
if (argc >= 2) inputFilename = argv[1];
+
if (argc >= 3) outputFilename = argv[2];
+
OT.open(inputFilename);
if (!OT.is_open()) {
+
cout << inputFilename << ": failed to open file";
return 1;
}
···
cout << "the book: ";
getline(cin, book);
+
for (int i = 0; i < book.length(); i++)
+
normalizedBook += toupper(book[i]);
cout << "the chapter: ";
cin >> chapter;
cout << "the verse: ";
cin >> verse;
+
cout << endl;
+
bool foundBook = false;
+
bool foundChapter = false;
+
bool foundVerse = false;
+
string verseString;
+
while (!OT.eof()) {
+
string input;
+
+
if (!foundBook) {
+
OT >> input; // THE
+
if (input != "THE") continue;
+
OT >> input; // BOOK
+
if (input != "BOOK") continue;
+
OT >> input; // OF
+
if (input != "OF") continue;
+
+
OT.ignore(); // skip the single space
+
+
getline(OT, input); // handle multi word books
+
+
if (input == normalizedBook) {
+
foundBook = true;
+
+
getline(OT, input); // discard empty line
+
continue;
+
}
+
} else {
+
if (!foundChapter) {
+
OT >> input;
+
if (input != "CHAPTER") {
+
if (input == "THE") break;
+
+
continue;
+
}
+
OT >> input;
+
if (input == to_string(chapter)) {
+
foundChapter = true;
+
continue;
+
}
+
} else {
+
OT >> input;
+
+
if (input == "CHAPTER") break;
+
+
if (input == to_string(verse)) {
+
foundVerse = true;
+
OT.ignore(); // ignore space
+
getline(OT, verseString);
+
break;
+
}
+
}
+
}
+
}
+
+
if (foundVerse) {
+
cout << verse << " " << verseString << endl;
+
+
OF.open(outputFilename);
+
+
if (!OF.is_open()) {
+
cout << outputFilename << ": error saving file" << endl;
+
return 1;
+
}
+
+
OF << verse << " " << verseString << endl;
+
+
OF.close();
+
} else {
+
if (!foundBook) {
+
cout << book << " does not exist in the Old Testament";
+
} else {
+
if (!foundChapter) {
+
cout << "Chapter " << chapter << " does not exist in " << book;
+
} else if (!foundVerse) {
+
cout << "Verse " << verse << " does not exist in " << book << " " << chapter;
+
}
+
}
+
+
cout << endl;
+
}
OT.close();