My program won't do what I want it to do

Objective: Develop a C++ program which when given a Bible reference (i.e., book, chapter, and verse) reads a file of Bible text to find and prints the referenced verse.

Discussion: In this assignment you will become familiar with file I/O by reading and writing files using the C++ fstream library. This library allows you to use the familiar “>>” and “<<” operators for input and output on files.
To complete this assignment your program should prompt for a Bible reference: that is a book, chapter and verse. The Bible book name should be given as a full name (if you would like to use abbreviations, please see extra credit opportunity below). The program should then search a file of Bible text (e.g., Bible.txt) for the referenced verse. If the verse is found, then the verse should be printed to the screen and appended to an output file (verses.txt). If the verse is not found (e.g., the book was misspelled or the chapter or verse reference was incorrect), then the program should report that the verse was not found, because the book, chapter or verse (whichever applies) was not in the Bible.
In /home/faculty/ggreve/CS1210/public you can find an example of this program and the Bible text. The program is called hw5 and the file of Bible text is called Bible.txt
Please also take note of some other program details addressed in the section “Additional Details” on the next page.

Additional Details:
Note: Your program must meet the following additional requirements:
It must append the requested verses, if found, to an output file. The output file should be named “verses.txt”.
Your program must report when a verse cannot be found and why. This error message should NOT be appended to the output file.
Be sure to test you program with different verses to make sure your program is robust. For example, you might try to find the first verse of a book, or the last, or the first verse from a chapter. You should also try various books. I recommend you try to find verses in the Psalms—particularly Psalm 119.
Submit your source code to your instructor by the beginning of class on due date. Turn in a listing of your program in class that same day.

Example: An example run of hw5 is shown below. It includes an example of retrieving a valid reference and shows the errors the program reports for invalid requests:
john% >hw5
Please enter reference of the verse you would like to retrieve
the book: John
the chapter: 3
the verse: 16
The verse you want is:
John 3:16 For God so loved the world, that he gave his only begotten Son,
that whosoever believeth in him should not perish, but have
everlasting life.
john% >hw5
Please enter reference of the verse you would like to retrieve
the book: Hezekiah
the chapter: 1
the verse: 1
The Bible does not contain the book "Hezekiah"
john% >hw5
Please enter reference of the verse you would like to retrieve
the book: Jude
the chapter: 2
the verse: 1
The book Jude does not have chapter 2
john%temp>hw5
Please enter reference of the verse you would like to retrieve
the book: Philemon
the chapter: 1
the verse: 30
Chapter 1 of book Philemon does not have verse 30

This is my code so far. It compiles and runs the words and input but won't output onto the screen the verse I try to get. Any help would be amazing. this is due tomorrow at 1.... so any quick help or tips would be amazing!

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
  #include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
#include <string>
using namespace std;

int main(){
        string bookName, chapter, verse;
        int foundBook, foundChapter, foundVerse;
        ifstream in;
        ofstream out;

        in.open("Bible.txt");

        if (in.fail()){
                cout << "Error while opening the Bible.\n";
                exit(1);
        }

        out.open("verses.txt");

        if (out.fail()){
                cout << "Error while creating the verses file\n";
                exit(1);
        }

       cout << "Please enter the reference of the "
             << "verse you would like to retrieve\n"
             << "the book:    " << endl;
        cin >> bookName;

        cout << "the chapter: " << endl;
        cin >> chapter;

        cout << "the verse:   " << endl;
        cin >> verse;

        while (in >> foundBook){
                in >> chapter;
                in >> verse;

                if (bookName == chapter){
                        cout << foundBook;
                        chapter = foundBook;
                }

                if (bookName == verse){
                        cout << foundBook;
                        verse = foundBook;
                }
        }

        for (int i = 0; i < bookName.size(); i++){
                bookName[i] = toupper (bookName[i]);

                cout << bookName;

                string line;
                getline(in, line, '\n');

                while (!foundBook && !in.eof()){

                        if (line.substr(0,11) == "THE BOOK OF " + bookName){
                                if (line.substr(13,bookName.size())==bookName){

                                }
                        }
                }
        }
        return 0;
}
I think we would need a section of the bible.txt to give good advise.
THE 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.
11 And God said, Let the earth bring forth grass, the herb yielding seed, [and] the fruit tree yielding fruit after his kind, whose seed [is] in itself, upon the earth: and it was $
12 And the earth brought forth grass, [and] herb yielding seed after his kind, and the tree yielding fruit, whose seed [was] in itself, after his kind: and God saw that [it was] go$
13 And the evening and the morning were the third day.
14 And God said, Let there be lights in the firmament of the heaven to divide the day from the night; and let them be for signs, and for seasons, and for days, and years:
15 And let them be for lights in the firmament of the heaven to give light upon the earth: and it was so.
16 And God made two great lights; the greater light to rule the day, and the lesser light to rule the night: [he made] the stars also.
17 And God set them in the firmament of the heaven to give light upon the earth,
18 And to rule over the day and over the night, and to divide the light from the darkness: and God saw that [it was] good.
19 And the evening and the morning were the fourth day.
20 And God said, Let the waters bring forth abundantly the moving creature that hath life, and fowl [that] may fly above the earth in the open firmament of heaven.
21 And God created great whales, and every living creature that moveth, which the waters brought forth abundantly, after their kind, and every winged fowl after his kind: and God s$
22 And God blessed them, saying, Be fruitful, and multiply, and fill the waters in the seas, and let fowl multiply in the earth.
23 And the evening and the morning were the fifth day.
24 And God said, Let the earth bring forth the living creature after his kind, cattle, and creeping thing, and beast of the earth after his kind: and it was so.
25 And God made the beast of the earth after his kind, and cattle after their kind, and every thing that creepeth upon the earth after his kind: and God saw that [it was] good.
26 And God said, Let us make man in our image, after our likeness: and let them have dominion over the fish of the sea, and over the fowl of the air, and over the cattle, and over $
27 So God created man in his [own] image, in the image of God created he him; male and female created he them.
28 And God blessed them, and God said unto them, Be fruitful, and multiply, and replenish the earth, and subdue it: and have dominion over the fish of the sea, and over the fowl of$
29 And God said, Behold, I have given you every herb bearing seed, which [is] upon the face of all the earth, and every tree, in the which [is] the fruit of a tree yielding seed; t$
30 And to every beast of the earth, and to every fowl of the air, and to every thing that creepeth upon the earth, wherein [there is] life, [I have given] every green herb for meat$
31 And God saw every thing that he had made, and, behold, [it was] very good. And the evening and the morning were the sixth day.

CHAPTER 2
1 Thus the heavens and the earth were finished, and all the host of them.
2 And on the seventh day God ended his work which he had made; and he rested on the seventh day from all his work which he had made.
3 And God blessed the seventh day, and sanctified it: because that in it he had rested from all his work which God created and made.
4 These [are] the generations of the heavens and of the earth when they were created, in the day that the LORD God made the earth and the heavens,
5 And every plant of the field before it was in the earth, and every herb of the field before it grew: for the LORD God had not caused it to rain upon the earth, and [there was] no$
6 But there went up a mist from the earth, and watered the whole face of the ground.
7 And the LORD God formed man [of] the dust of the ground, and breathed into his nostrils the breath of life; and man became a living soul.
8 And the LORD God planted a garden eastward in Eden; and there he put the man whom he had formed.
9 And out of the ground made the LORD God to grow every tree that is pleasant to the sight, and good for food; the tree of life also in the midst of the garden, and the tree of kno$
10 And a river went out of Eden to water the garden; and from thence it was parted, and became into four heads.
11 The name of the first [is] Pison: that [is] it which compasseth the whole land of Havilah, where [there is] gold;
12 And the gold of that land [is] good: there [is] bdellium and the onyx stone.
13 And the name of the second river [is] Gihon: the same [is] it that compasseth the whole land of Ethiopia.
14 And the name of the third river [is] Hiddekel: that [is] it which goeth toward the east of Assyria. And the fourth river [is] Euphrates.
15 And the LORD God took the man, and put him into the garden of Eden to dress it and to keep it.
15 And the LORD God took the man, and put him into the garden of Eden to dress it and to keep it.
16 And the LORD God commanded the man, saying, Of every tree of the garden thou mayest freely eat:
17 But of the tree of the knowledge of good and evil, thou shalt not eat of it: for in the day that thou eatest thereof thou shalt surely die.
18 And the LORD God said, [It is] not good that the man should be alone; I will make him an help meet for him.
19 And out of the ground the LORD God formed every beast of the field, and every fowl of the air; and brought [them] unto Adam to see what he would call them: and whatsoever Adam c$
20 And Adam gave names to all cattle, and to the fowl of the air, and to every beast of the field; but for Adam there was not found an help meet for him.
21 And the LORD God caused a deep sleep to fall upon Adam, and he slept: and he took one of his ribs, and closed up the flesh instead thereof;
22 And the rib, which the LORD God had taken from man, made he a woman, and brought her unto the man.
23 And Adam said, This [is] now bone of my bones, and flesh of my flesh: she shall be called Woman, because she was taken out of Man.
24 Therefore shall a man leave his father and his mother, and shall cleave unto his wife: and they shall be one flesh.
25 And they were both naked, the man and his wife, and were not ashamed.

The '$' means it is going on more than you can see but that shouldn't mess with anything. This is how it starts out and it has the whole bible so its a super long file.
Lines 31 and 39 are wrong. Use getline. (Although you'll have to do something about the newline hanging around in the input stream when line 39 is reached.)
I'm not really sure what is wrong, could you explain to me why it would be wrong? I am very new to this.
A book name may have more than one token (or word) in it. Using the extraction operator (>>) will only extract a single token from the input stream it acts on.
The first problem as Cire stated above is your input section.
add
cout << bookName << ": " << chapter << ": " << verse << endl;
to line 38 and get your input working before you try reading the file.

Oh ok that makes sense. so should I change line 31 and 39 to a getline statement? do I do that by defining a new string?
I would change all your input operations to use getline. Then, you won't have to deal with mixing formatted and unformatted input extraction. The text in your file seems to be very conveniently formatted for line-by-line processing as well.
oh ok I see. Thank you very much for your help!
Topic archived. No new replies allowed.