Created
June 9, 2011 01:00
-
-
Save qpfiffer/1015808 to your computer and use it in GitHub Desktop.
Help
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <fstream> | |
#include <iomanip> | |
using namespace std; | |
const int N = 16; | |
struct Names | |
{ | |
string author; | |
string title; | |
} people[N]; | |
int loadData(string, int &, int &); | |
void showAll(int); | |
char menu(); | |
void searchByName(int, string); | |
void searchByTitle(int, string); | |
int main() | |
{ | |
ofstream outFile; | |
string filePath; | |
int authorCount = 0, bookCount = 0; | |
char option = ' '; | |
string temp; | |
//gets the file path from user | |
cout << "Please enter the path of the text file: " << endl; | |
//getline(cin, filePath); | |
filePath = ("library.txt"); | |
//calls loadData function to load the text file | |
loadData(filePath, authorCount, bookCount); | |
showAll(authorCount); | |
//checks the menu input | |
while (1) | |
{ | |
option = menu(); | |
if(toupper(option) == 'S') | |
showAll(authorCount); | |
else if(toupper(option) == 'A') | |
{ | |
cout << "Please enter the name of the author you wish to find: " << endl; | |
if (!std::getline(std::cin, temp)) | |
break; | |
//cin >> temp; | |
searchByName(authorCount, temp); | |
} | |
else if(toupper(option) == 'T') | |
{ | |
cout << "Please enter the title of the book you wish to find: "; | |
getline(cin, temp); | |
searchByTitle(authorCount, temp); | |
} | |
else if (toupper(option) == 'Q') | |
break; | |
} | |
return 0; | |
} | |
int loadData(string filePath, int &authorCount, int &bookCount) | |
{ | |
ifstream inFile; | |
string empty; | |
inFile.open(filePath); | |
if(!inFile) | |
return -1; | |
else | |
{ | |
while(inFile.peek() != EOF) | |
{ | |
getline(inFile, empty); | |
authorCount++; | |
} | |
authorCount = authorCount / 2; | |
bookCount = authorCount; | |
//resets file. | |
inFile.clear(); | |
inFile.seekg(0); | |
while(!inFile.eof()) | |
{ | |
for(int i = 0; i < authorCount; i++) | |
{ | |
getline(inFile, people[i].title); | |
getline(inFile, people[i].author); | |
} | |
} | |
} | |
cout << "Number of records that have been successfully loaded: " << bookCount << endl; | |
inFile.close(); | |
return 0; | |
} | |
void showAll(int authorCount) | |
{ | |
for(int i = 0; i < authorCount; i++) | |
cout << left << people[i].title << " " << '(' << people[i].author << ')' << endl; | |
cout << "Number of Records found: " << authorCount << endl; | |
} | |
char menu() | |
{ | |
char option; | |
string temp; | |
cout << endl << endl << "To search by author enter (A), to search by title enter (T), " | |
<< "to show all authors and books enter (S), or to quit enter (Q)" << endl << endl; | |
cin >> option; | |
getline(cin, temp); | |
return option; | |
} | |
void searchByName(int authorCount, string temp) | |
{ | |
int number = 0; | |
for(int i = 0; i < authorCount; i++) | |
{ | |
if(temp.compare(people[i].author) == 0) | |
{ | |
cout << left << people[i].title << " " << '(' << people[i].author << ')' << endl; | |
number++; | |
} | |
} | |
cout << number << " Records found." << endl << endl; | |
} | |
void searchByTitle(int authorCount, string temp) | |
{ | |
int number = 0; | |
for(int i = 0; i < authorCount; i++) | |
{ | |
if(temp.compare(people[i].title) == 0) | |
{ | |
cout << left << people[i].title << " " << '(' << people[i].author << ')' << endl; | |
number++; | |
} | |
} | |
cout << number << " Records found." << endl << endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment