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 if (normalizedBook == "PSALMS")
47 normalizedBook = "PSALM";
48
49 cout << "the chapter: ";
50 cin >> chapter;
51
52 cout << "the verse: ";
53 cin >> verse;
54
55 cout << endl;
56
57 bool foundBook = false;
58 bool foundChapter = false;
59 bool foundVerse = false;
60 string verseString;
61 while (!OT.eof()) {
62 string input;
63
64 if (!foundBook) {
65 OT >> input; // THE
66 if (input != "THE") continue;
67 OT >> input; // BOOK
68 if (input != "BOOK") continue;
69 OT >> input; // OF
70 if (input != "OF") continue;
71
72 OT.ignore(); // skip the single space
73
74 getline(OT, input); // handle multi word books
75
76 // second check is for the book of psalms
77 if (input == normalizedBook || input == (normalizedBook + "S")) {
78 foundBook = true;
79
80 getline(OT, input); // discard empty line
81 continue;
82 }
83 } else {
84 if (!foundChapter) {
85 OT >> input;
86 if (input != "CHAPTER" && input != "PSALM") {
87 if (input == "THE") break;
88
89 continue;
90 }
91
92 OT >> input;
93 if (input == to_string(chapter)) {
94 foundChapter = true;
95 continue;
96 }
97 } else {
98 OT >> input;
99
100 if (input == "CHAPTER" || input == "PSALM") break;
101
102 if (input == to_string(verse)) {
103 foundVerse = true;
104 OT.ignore(); // ignore space
105 getline(OT, verseString);
106 break;
107 }
108 }
109 }
110 }
111
112 if (foundVerse) {
113 cout << verse << " " << verseString << endl;
114
115 OF.open(outputFilename, ios_base::app);
116
117 if (!OF.is_open()) {
118 cout << outputFilename << ": error saving file" << endl;
119 return 1;
120 }
121
122 OF << verse << " " << verseString << endl;
123
124 OF.close();
125 } else {
126 if (!foundBook) {
127 cout << (normalizedBook == "PSALM" ? book += "s" : book) << " does not exist in the Old Testament";
128 } else {
129 if (!foundChapter) {
130 cout << (normalizedBook == "PSALM" ? "Psalm " : "Chapter ") << chapter;
131 cout << " does not exist in " << (normalizedBook == "PSALM" ? book += "s" : book);
132 } else if (!foundVerse) {
133 cout << "Verse " << verse << " does not exist in " << book << " " << chapter;
134 }
135 }
136
137 cout << endl;
138 }
139
140 OT.close();
141
142 return 0;
143}