a gleam implementation of a CS assignment originally written in cpp
1/********************************************************* 2* Summary: find verses from the old testament 3* appending them to file as found 4* 5* Author: Kieran Klukas 6* Created: October 2025 7* 8********************************************************/ 9 10#include <iostream> 11#include <cstdlib> 12#include <fstream> 13 14using namespace std; 15 16int main(int argc, char **argv) { 17 18 ifstream OT; 19 ofstream OF; 20 string inputFilename = "../test/OT.txt"; 21 string outputFilename = "build/verses.txt"; 22 23 string book; 24 string normalizedBook; 25 int chapter; 26 int verse; 27 28 // get path to OT.txt if provided otherwise use default path 29 // and then do the same for the verses.txt 30 if (argc >= 2) inputFilename = argv[1]; 31 if (argc >= 3) outputFilename = argv[2]; 32 33 OT.open(inputFilename); 34 35 if (!OT.is_open()) { 36 cout << inputFilename << ": failed to open file"; 37 return 1; 38 } 39 40 cout << "Please enter the reference of the verse you would like" << endl; 41 42 cout << "the book: "; 43 getline(cin, book); 44 for (int i = 0; i < book.length(); i++) 45 normalizedBook += toupper(book[i]); 46 47 cout << "the chapter: "; 48 cin >> chapter; 49 50 cout << "the verse: "; 51 cin >> verse; 52 53 cout << endl; 54 55 bool foundBook = false; 56 bool foundChapter = false; 57 bool foundVerse = false; 58 string verseString; 59 while (!OT.eof()) { 60 string input; 61 62 if (!foundBook) { 63 OT >> input; // THE 64 if (input != "THE") continue; 65 OT >> input; // BOOK 66 if (input != "BOOK") continue; 67 OT >> input; // OF 68 if (input != "OF") continue; 69 70 OT.ignore(); // skip the single space 71 72 getline(OT, input); // handle multi word books 73 74 if (input == normalizedBook) { 75 foundBook = true; 76 77 getline(OT, input); // discard empty line 78 continue; 79 } 80 } else { 81 if (!foundChapter) { 82 OT >> input; 83 if (input != "CHAPTER") { 84 if (input == "THE") break; 85 86 continue; 87 } 88 OT >> input; 89 if (input == to_string(chapter)) { 90 foundChapter = true; 91 continue; 92 } 93 } else { 94 OT >> input; 95 96 if (input == "CHAPTER") break; 97 98 if (input == to_string(verse)) { 99 foundVerse = true; 100 OT.ignore(); // ignore space 101 getline(OT, verseString); 102 break; 103 } 104 } 105 } 106 } 107 108 if (foundVerse) { 109 cout << verse << " " << verseString << endl; 110 111 OF.open(outputFilename); 112 113 if (!OF.is_open()) { 114 cout << outputFilename << ": error saving file" << endl; 115 return 1; 116 } 117 118 OF << verse << " " << verseString << endl; 119 120 OF.close(); 121 } else { 122 if (!foundBook) { 123 cout << book << " does not exist in the Old Testament"; 124 } else { 125 if (!foundChapter) { 126 cout << "Chapter " << chapter << " does not exist in " << book; 127 } else if (!foundVerse) { 128 cout << "Verse " << verse << " does not exist in " << book << " " << chapter; 129 } 130 } 131 132 cout << endl; 133 } 134 135 OT.close(); 136 137 return 0; 138}