Return to Snippet

Revision: 43081
at March 16, 2011 09:14 by Sleaker


Initial Code
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

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);


int main()
{
	char choice = ' ';
	ifstream inFile;
	int count = 0;

	string fileName;

	cout << "Welcome to Patrick’s Library Database." << endl << "Please enter the name of the backup file:" << endl;
	getline(cin, fileName);
	count = loadData(fileName);
	if ( count < 1 )
	{
		cout << endl << "Failed to load " << fileName << ". Eixiting now";
		choice = 'q';
	}

	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)
	{ 
		cout << "File did not open! ";
		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 << endl << books[i].title << " by " << books[i].author;
	}
}

void showBooksByAuthor (int count, string author)
{
	//int authorOrder;
	//int location;
	//for(authorOrder >= 'a'; authorOrder < name; authorOrder++)
}
void showBooksByTitle (int count, string title)
{
	
}


void sortByTitle (int count)
{
	/*
	int i, j, minIndex ;
	string temp;
	for(i = 0; i < size - 1; i++)
	{
		minIndex = i;
		for(j = i + 1; j < size; j++)
		{
			if(strcmp(title[j].c_str(), title[minIndex].c_str()) < 0)
				minIndex = j;
		}
		swap(title, minIndex, i);
		
		temp = title[i];
		title[i] = title[min];
		title[min] = temp;
	}
	*/
	return;
}

void sortByAuthor (int count)
{
	/*
	int i, j, minIndex ;
	string temp;
	for(i = 0; i < size - 1; i++)
	{
		minIndex = i;
		for(j = i + 1; j < size; j++)
		{
			if(strcmp(author[j].c_str(), author[minIndex].c_str()) < 0)
				minIndex = j;
		}
		swap(author, minIndex, i);
		
		temp = author[i];
		author[i] = author[min];
		author[min] = temp;
	}
	*/
     return;
}

Initial URL


Initial Description


Initial Title
fun with structs

Initial Tags


Initial Language
C++