Searching a txt file output

Hey everyone, I have to search a txt file and output what the user wants. Specifically...

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. Examples for using the fstream library appear in display 6.2 (pg 317) and the file I/O summary (pg 318) in your textbook. You can also find another example (file_io_example.cpp) on our class web.
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 below). The program should then search a file of Bible text for the referenced verse. If the verse is found, then the verse should be printed to the screen and appended to an output file. 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. You can find an example of this program and the file of Bible text at /home/vfang/public. The program is called hw8 and the file of Bible text is called KJV_Bible.txt.
Please also take note of some other program details addressed in the section “Additional Details” on the next page.

This is what I have so far, but I'm stuck on what to do next

#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <iomanip>

using namespace std;

int main(){

// declare variables
char string bookName, chapter[], verse[];
bool foundBook, foundChapter, foundVerse;
ifstream in;
ofstream out;

//get bible"Bible.txt");

cerr<<"Error while opening the Bible\n";

// open verses.txt & test"verses.txt");

cerr<<"Error while creating the verses file\n";

//ask for input
cout<<"Please input:\nThe book: "<<endl;
cout<<"The chapter: "<<endl;
cout<<"The verses: "<<endl;

// start search
for(int i=0;i<bookName.size();i++)
string line;
while(!foundBook && !in.eof()){
if(line.substr(0,11)=="THE BOOK OF"){

return 0;

Any help is greatly appreciated!
It would be of great help if you post the Bible file format or a sample of the file where it is possible to understand the format. Ciao.
P.S. Please, do not forget to indent your code:)
Last edited on
Good point, here is an example of how it should run. Not sure if this helps or not

john% >hw14
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% >hw14
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% >hw14
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
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
Sorry, but what would really help is the KJV_Bible.txt format or extract. Otherwise, it is difficult to validate your code and to suggest improvements. Ciao
Topic archived. No new replies allowed.