how do i get this function to search the array for a title or author and show it, anyone any ideas

void recordArray(BookRecord *book, fstream &dataFile)
{
dataFile.open("Books.txt",ios::out | ios::app); // Open File to store array
int index = 1; //Index needs a staring point so it dose not have a garbage number to start with or loop wont end till the garbage number is reached
char again;//Needed to end the loop
BookRecord *bkPtr = new BookRecord[index]; // Declare a pointer(bkPtr) to point to BookRecord and Dynamically Allocate BookRecord (it will still allocate new elements even though index was given a size)

do
{
for(int i = 0; i<index; i++)//"i" is the same throughout the for loop until it is incremented thus storing in each variable throughout the structure
{

cin.ignore();//Clears the buffer before hand because cin >> was used and left a newline character in the input stream (a very common and forgettable mistake)
cout << "Enter Title \n";
getline(cin, bkPtr[i].title);//getline for string
dataFile << bkPtr[i].title;//transfers the retrieved data and saves it to the file
cout << "\nEnter Author \n";
getline(cin, bkPtr[i].author);
dataFile << bkPtr[i].author;
cout << "\nEnter ISBN \n";
getline(cin, bkPtr[i].ISBN);
dataFile << bkPtr[i].ISBN;
cout << "\nStatus of book: enter Yes for in or No for out \n";
getline(cin, bkPtr[i].status);
dataFile << bkPtr[i].status;
cout << "\nTo add another book enter Y or enter N to save and exit to Main Menu \n";
cin >> again;//Used to exit loop or continue
}
}while(again !='n' && again !='N');

cin.ignore(); // to clear anything remaining in the buffer (is just good practice to be sure)
dataFile.close(); // saves and closes file
return; // returns back to main function
}

//////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
void srchDisplay(BookRecord *book, fstream &dataFile)
{
string userInput;// variable to hold the input that will compare to the array
dataFile.open("Books.txt", ios::in);// Reads in data from file
int index = 1;
char again;
BookRecord *bkPtr = new BookRecord[index];

do
{
cout << "Please enter a title or author name to see if the book is listed \n";
cin.ignore();
getline(cin, userInput);

for(int i=0; i<index; i++)
{
if(userInput == bkPtr[i].title || userInput == bkPtr[i].author)
{
dataFile >> bkPtr[i].title;
cout << bkPtr[i].title << endl;
dataFile >> bkPtr[i].author;
cout << bkPtr[i].author << endl;
dataFile >> bkPtr[i].ISBN;
cout << bkPtr[i].ISBN << endl;
dataFile >> bkPtr[i].status;
cout << bkPtr[i].status << endl;
cout << "\nTo search another book enter Y or enter N to save and exit to Main Menu \n";
cin >> again;//Used to exit loop or continue
}
else
{
cout << "No Matches, would you like to search again? Y for yes or N for no \n";
cin >> again;
}
}
}while(again !='n'&& again !='N');

return;
You need to move the "No Matches" branch outside of the loop:
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
do
{
cout << "Please enter a title or author name to see if the book is listed \n";
cin.ignore();
getline(cin, userInput);

bool is_match = false; // Note
for(int i=0; i<index; i++)
{
if(userInput == bkPtr[i].title || userInput == bkPtr[i].author)
{
is_match = true; // Note
dataFile >> bkPtr[i].title;
cout << bkPtr[i].title << endl;
dataFile >> bkPtr[i].author;
cout << bkPtr[i].author << endl;
dataFile >> bkPtr[i].ISBN;
cout << bkPtr[i].ISBN << endl;
dataFile >> bkPtr[i].status;
cout << bkPtr[i].status << endl;
cout << "\nTo search another book enter Y or enter N to save and exit to Main Menu \n";
cin >> again;//Used to exit loop or continue
}
}
if(is_match) // Note
  again = 'n';
else
{
cout << "No Matches, would you like to search again? Y for yes or N for no \n";
cin >> again;
}
}while(again !='n'&& again !='N');
Topic archived. No new replies allowed.