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 = "OT.txt";
21 string outputFilename = "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 // second check is for the book of psalms
75 if (input == normalizedBook || input == (normalizedBook + "S")) {
76 foundBook = true;
77
78 getline(OT, input); // discard empty line
79 continue;
80 }
81 } else {
82 if (!foundChapter) {
83 OT >> input;
84 if (input != "CHAPTER" && input != "PSALM") {
85 if (input == "THE") break;
86
87 continue;
88 }
89
90 OT >> input;
91 if (input == to_string(chapter)) {
92 foundChapter = true;
93 continue;
94 }
95 } else {
96 OT >> input;
97
98 if (input == "CHAPTER" || input == "PSALM") break;
99
100 if (input == to_string(verse)) {
101 foundVerse = true;
102 OT.ignore(); // ignore space
103 getline(OT, verseString);
104 break;
105 }
106 }
107 }
108 }
109
110 if (foundVerse) {
111 cout << verse << " " << verseString << endl;
112
113 OF.open(outputFilename, ios_base::app);
114
115 if (!OF.is_open()) {
116 cout << outputFilename << ": error saving file" << endl;
117 return 1;
118 }
119
120 OF << verse << " " << verseString << endl;
121
122 OF.close();
123 } else {
124 if (!foundBook) {
125 cout << (normalizedBook == "PSALM" ? book += "s" : book) << " does not exist in the Old Testament";
126 } else {
127 if (!foundChapter) {
128 cout << (normalizedBook == "PSALM" ? "Psalm " : "Chapter ") << chapter;
129 cout << " does not exist in " << (normalizedBook == "PSALM" ? book += "s" : book);
130 } else if (!foundVerse) {
131 cout << "Verse " << verse << " does not exist in " << book << " " << chapter;
132 }
133 }
134
135 cout << endl;
136 }
137
138 OT.close();
139
140 return 0;
141}