Hi,
I tried the infile.seekg(0, ios::beg); and it compiled fine but i'm not getting the result I was hoping for. Think you can take a look at my function and find the bug? I've got to figure out how to debug.
So far the program does a good job of scanning through verses.txt and lists the references for the verses in this file but then I want the user to be able to choose one of the verses in the text file and have it display the chosen reference and verse contents on the screen.
Here's a sample of the text file contents:
Code:
|Romans 3:23
For all have sinned
and fall short
of the glory of
God
|acts 1:8
and you shall receive
power when the Holy
Spirit comes upon you
and you shall be my witnesses
|Romans 1:21
For me to live is Christ
and to die is gain
and here is my function...
Code:
void SearchForVerse()
{
ifstream infile("verses.txt");
string refs[30], dummy, chosenRef, aRef;
int i = 0, //counter
pipePos, //pipe position in string
verseChoice; //which verse to display
system("cls");
cout << "Here are the references for the verses stored in verses.txt...\n\n";
while(!infile.eof())
{
getline(infile, dummy, '\n'); //read in line up to new line char
int dummySize = dummy.size(); //compute size of string
pipePos = dummy.find('|', 0); //find pipe starting at position 0
if(pipePos == 0) //if the first char is a pipe...
{
refs[i] = dummy.substr(1, dummySize); //take the rest of string as a ref
i++; //increment for ref array
}
}
for(int p=0; p<i; p++)
cout << p+1 << ". " << refs[p] << endl;
//Works great up to this point!
//The next chunk of code doesn't do anything...just a couple of pauses.
cout << "\n\nChoose a verse to display: " << flush;
cin >> verseChoice; //eventually will have error-checking here
infile.seekg(0, ios::beg); //seek to 0 bytes from the begin. of the file
//is this doing what i want it to do?
while(!infile.eof())
{
getline(infile, dummy, '\n');
cout << "inside while loop, read into dummy: " << dummy << endl;
system("pause");
//int dummySize = dummy.size(); //compute size of string
pipePos = dummy.find('|', 0); //find pipe starting at position 0
if(pipePos == 0) //This is definitely a reference
{
aRef = dummy.substr(1, dummy.size()); //take the rest of string as a ref
if(aRef == refs[verseChoice-1]) //if ref. found in file == chosen ref.
{
cout << "\n\nYou chose: " << aRef << "\n\n";
for(int b=0; b<5; b++)
{
getline(infile, dummy, '\n');
cout << dummy << endl;
}
}
else
cout << "Could not find the reference you chose." << endl;
cout << "\n\n";
system("pause");
} //end if
} //end while
infile.close();
system("pause");
}
Thanks for any help you can throw my way.