Problem with my Code

The goal of this code is for the user to input the Book, chapter and verse of the bible and my program will search an input file and return the verse to the user. This verse will then be stored in an output file. (the teacher assigned this exercise for us to work in fstream).

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
int main(){
	// Declare Vairables
	bool bookFound = true;
	bool chapterFound = true;
	string line;
	string bookName;
	string chapter, verse;
	ifstream in;
	ofstream out;
	// Opens the file
	in.open("bible.txt");

	if (in==0)
		{ 
			cerr << "Error! File not found" << "\n";
			exit(1);
		}
	//open (verses.txt & test)

	//ask input
	cout << "Please enter reference of the verse you would like to retrieve." << "\n";
	cout << "Book: " ;
	in > getline(cin, bookName);
	cout<< "\n" << "Chapter: " ;
	cin >> chapter;
	cout << "\n" << "Verse: ";
	cin >> verse;
	// Convert the Book Name to Capital letters
	for (int i = 0; i < bookName.size(); i++)
	bookName[i] = toupper(bookName[i]);
// Search the Book
	getline(in,line, '\n');
	while(bookFound == false && in.eof() == false)
	{
		if(line.substr(0, 11) == "THE BOOK OF")
		{
 			if(line.substr(12, bookName.size()) == bookName) 
				{	
					bookFound = true;
					getline(in,line);
				}
		}
		else
			cout << bookName << " not found.";
	}
	if(bookFound == true) // Looks to see if the book is true
			{
				while(chapterFound == false && in.eof() == false)
				{
					if (line.substr(0,7) == "CHAPTER")
						{
 							if(line.substr(8, chapter.size()) == chapter) 
							{	
								chapterFound = true;
								getline(in,line, '\n');
							}
							else
								cout << "Chapter " << chapter << " not found.";
						}
				}
			} 
						
	if(chapterFound == true)
		{
			if (line.substr(0,1) == verse)
			{
				size_t found = line.find_first_not_of("abcdefghijklmnopqrstuvwyz.[] "); // looks for the next number to stop at
				cout << line.substr(0, found);	
				out.open("verses.txt", ios::app); // should create a file to store the output
				out << line; // should store the information into the file ^^
			}
			else
				cout << "Invalid verse number";
		}
}


Part of the Input File
1
2
3
4
5
6
7
8
9
10
11
12
13
BOOK OF GENESIS

CHAPTER 1
1 In the beginning God created the heaven and the earth.
2 And the earth was without form, and void; and darkness [was] upon the face of the deep. And the Spirit of God moved upon the face of the waters.
3 And God said, Let there be light: and there was light.
4 And God saw the light, that [it was] good: and God divided the light from the darkness.
5 And God called the light Day, and the darkness he called Night. And the evening and the morning were the first day.
6 And God said, Let there be a firmament in the midst of the waters, and let it divide the waters from the waters.
7 And God made the firmament, and divided the waters which [were] under the firmament from the waters which [were] above the firmament: and it was so.
8 And God called the firmament Heaven. And the evening and the morning were the second day.
9 And God said, Let the waters under the heaven be gathered together unto one place, and let the dry [land] appear: and it was so.
10 And God called the dry [land] Earth; and the gathering together of the waters called he Seas: and God saw that [it was] good.


my problem is that it doesn't print anything nor create the output file...

Any suggestions?

*Edit*

I should point out that the input file is on a Linux server in my public folder... I pretty sure that its not an input problem..
Last edited on
When I run it, it outputs a whole bunch of stuff. If you're getting nothing out, the problem is not your code.


Please enter reference of the verse you would like to retrieve.
Book: GENESIS

Chapter: 1

Verse: 1
Invalid verse number
haha the problem is that there is a verse 1 in Genesis
The problem is that you get no output and I get the output above. Once that's sorted, then we can fix the code.
Last edited on
Topic archived. No new replies allowed.