/ Published in: C++
oh gawd. the extra two functions were just there for reference. very incomplete. Also, there are inconsistencies in the variables simply because I'm pulling these straight from my notes. Still trying to figure out how to apply it to the assignment.
Expand |
Embed | Plain Text
#include <iostream> #include <string> #include <fstream> using namespace std; struct temp { string temp; }; struct Book { string title; string author; }; const int ARRAY_SIZE = 1000; Book books [ARRAY_SIZE]; // Function prototypes int loadData(string pathName); void showAll(int count); void showBooksByAuthor (int count); void showBooksByTitle (int count); void sortByTitle (int count); void sortByAuthor (int count); void swap(int , int ); int main() { char choice = ' '; ifstream inFile; int count = 0; string fileName; cout << "Welcome to Patrick’s Library Database.'/n' Please enter the name of the backup file:" << endl; getline(cin, fileName); if ( loadData(fileName) == -1 ) { cout << "failed to load from file" << endl; return 0; } count = loadData(fileName); if(count < 1) loadData(fileName); while(choice != 'q' || choice != 'Q') { cout << "Enter Q to (Q)uit, Search (A)uthor, Search (T)itle, (S)how All:" << endl; cin >> choice; if(choice == 'q' || choice == 'Q') return 0; else if(choice == 'a' || choice == 'A') { sortByAuthor (count); showBooksByAuthor (count); } else if(choice == 't' || choice == 'T') { sortByTitle (count); showBooksByTitle (count); } else if(choice == 's' || choice == 'S') showAll(count); else { cout << "Invalid request. '/n' Enter Q to (Q)uit, Search (A)uthor, Search (T)itle, (S)how All: " << endl; cin >> choice; } } inFile.close(); //closes both files return 0; } int loadData(string pathName) { ifstream inFile; int i; inFile.open(pathName); if(!inFile) { return -1; } else if(inFile) { for(i = 0; !inFile.eof() && i < ARRAY_SIZE ; i++) { getline(inFile, books[i].title); getline(inFile, books[i].author); } return i; } return 0; } void showAll(int count) { for (int i = 0; i < count; i++) { cout << books[i].title << " by " << books[i].author << endl; } } void showBooksByAuthor (int count) { string author; cout << "Enter name or partial name of author: " << endl; cin >> author; for(int i = 0; i < count; i++) { if ( books[i].author.find(author) != -1 ) { cout << books[i].author << " " << books[i].title << endl; } } } void showBooksByTitle (int count) { string title; cout << "Enter name or partial name of title: " << endl; cin >> title; for(int i = 0; i < count; i++) { if ( books[i].title.find(title) != -1 ) { cout << books[i].title << " " << books[i].author << endl; } } } void sortByTitle (int count) { int i, j, minIndex ; for(i = 0; i < count - 1; i++) { minIndex = i; for(j = i + 1; j < count; j++) { if( books[j].title.compare( books[minIndex].title) < 0) minIndex = j; } swap(i, minIndex); } } void sortByAuthor (int count) { int i, j, minIndex ; for(i = 0; i < count - 1; i++) { minIndex = i; for(j = i + 1; j < count; j++) { if( books[j].author.compare( books[minIndex].author) < 0) minIndex = j; } swap(i, minIndex); } } void swap(int i, int minIndex) { Book temp; temp.title = books[i].title; temp.author = books[i].author; books[i].title = books[minIndex].title; books[i].author = books[minIndex].author; books[minIndex].title = temp.title; books[minIndex].author = temp.author; }
Comments
Subscribe to comments
You need to login to post a comment.

if( books[j].title.compare( books[minIndex].title) < 0)should be written asif( books[j].title < books[minIndex].title)