Subscribe to Invariance_Under_Translation        RSS Feed
-----

A new project: Sci-Fi Novel Cataloging program

Icon 1 Comments
Good Morning DIC

I recently returned from my vacation to home. While home, I spent some time thinking about my next project. I wanted to create something practical, that I could evolve into something useful. I noticed that my father has his science fiction collection scattered across many shelves, with no rational order made obvious. I also enjoy science fiction, and decided that I should write a program that my father could use to catalog his collection. Tonight I finished the first draft. It is a functioning program, but still requires a good scrub and the addition of features I would like to make available. I would like to share this first draft with you, and allow you to try it if you so wish. It needs some work, but at this phase it represents the idea of what this program is supposed to do. Enjoy, and I will bring you updates as I keep working.

-alias


UI.cpp
#include"includes misc.h"

using namespace Functions;
using namespace Variables;


class Node
{
	string author;
	string title;
	string date;
	friend class Book;
};


class Book
{
private:
	fstream obj;
	string author;
	string title;
	int date;
	string file_read;
	void Title_search();
	void Author_search();
	void Remove_book(Node &obj);
public:
	Book();
	Book(int i);
	~Book();
	void Cat_search();
	void Cat_view();
	void Cat_add_to();
	void Cat_remove();
	void Cat_help();
};

Book::Book(int i)
{
	author = " ";
	title = " ";
	date = 0;

	switch(i)
	{
	case 1:
		Cat_search();
		break;
	case 2:
		Cat_view();
		break;
	case 3:
		Cat_add_to();
		break;
	case 4:
		Cat_remove();
		break;
	case 5:
		Cat_help();
		break;
	case 6:
		break;
	default:
		Error("Error: Unknown option.");
	}
}

Book::Book()
{
}

Book::~Book()
{
}

void Book::Cat_search()
{
	int nav_choice = 0;
	Clear_screen();
	cout<<"--Search--"<<endl;
	cout<<"\nYou can either search by Title, or Author."<<endl;
	cout<<"1. Title \n"
		<<"2. Author "<<endl;
	cout<<"\nChoice: "; 
	if(!(cin>>nav_choice)) Error("Error: Incorrect Choice.");
	switch(nav_choice)
	{
	case 1:
		Title_search(); 
		break;
	case 2:
		Author_search();
		break;
	default:
		Error("Error: Unknown Option.");
	}
}

void Book::Title_search()
{
	Clear_screen();
	Node obj;
	cin.sync();
	string t_search = " ";
	string c_space = " ";
	cout<<"\nPlease enter the Title you are searching for: ";
	getline(cin, t_search);
	ifstream search("SF_Catalog.doc");
	while(search.is_open())
	{	
		getline(search, obj.title);
		getline(search, obj.author);
		getline(search, obj.date);
		getline(search, c_space);

		if(obj.title == t_search)
		{
			cout<<"\nFound match: \n\n"
				<< obj.title << endl
				<< obj.author << endl
				<< obj.date << endl
				<< endl;
		}
		if(search.eof()) break;
	}
	cout<<"\nPress any key to return to menu..."<<endl;
	cin.get();
	Clear_screen();
}

void Book::Author_search()
{
	Clear_screen();
	Node obj;
	cin.sync();
	string t_search = " ";
	string c_space = " ";
	cout<<"\nPlease enter the Author you are searching for: ";
	getline(cin, t_search);
	ifstream search("SF_Catalog.doc");
	do
	{	
		getline(search, title);
		if(title.size() == 0) break;
		obj.title = title;
		getline(search, author);
		if(author.size() == 0) break;
		obj.author = author;
		getline(search, obj.date);
		getline(search, c_space);

		if(obj.author == t_search)
		{
			cout<<"\nFound match: \n\n"
				<< obj.title << endl
				<< obj.author << endl
				<< obj.date << endl
				<< endl;
		}
	}while(!(search.eof()));
	cout<<"\nPress any key to return to menu..."<<endl;
	cin.get();
	Clear_screen();
}
	

void Book::Cat_view()
{
	Clear_screen();
	cout<<"--Full Catalog--\n"<<endl;
	ifstream obj("SF_Catalog.doc",ios::out);
	while(obj.is_open())
	{
		getline(obj, file_read);
		cout<< file_read <<endl;
		if(obj.eof()) break;
	}
	
	cout<<"\nPress any key to return to menu..."<<endl;
	cin.ignore();
	cin.get();
	Clear_screen();
}

void Book::Cat_add_to()
{
	Clear_screen();
	cout<<"--Add Entry--"<<endl;
	cin.sync();
	cout<<"\nEnter the Title: ";
	getline(cin,title);
	cout<<"\nEnter the Author: ";
	getline(cin,author);
	cout<<"\nEnter the Publication Date: ";
	cin>>date;
	ofstream obj("SF_Catalog.doc",::ios::app);     //Change to append or ate file
	if(!obj) Error("Error: Could not open file");
	obj << title <<endl
		<< author << endl
		<< date << endl
		<< endl;
	cout<<"\nEntry made."<<endl;
	cout<<"\nPress any key to return to menu..."<<endl;
	cin.ignore();
	cin.get();
	Clear_screen();
}


void Book::Cat_remove()
{
	Clear_screen();
	cin.sync();
	Node obj;
	string r_search = " ";
	string c_space = " ";
	char choice = ' ';
	cout<<"--Remove from Catalog--\n"<<endl;
	cout<<"You can search for a title to remove, "
		<<"please enter your search critera below.\n"<<endl;
	cout<<"Title: ";
	getline(cin, r_search);
	ifstream search("SF_Catalog.doc");
	while(search.is_open())
	{	
		getline(search, obj.title);
		getline(search, obj.author);
		getline(search, obj.date);
		getline(search, c_space);

		if(obj.title == r_search)
		{
			cout<<"\nLooking...\n"<<endl;
			cout<<"\nFound match: \n\n"
				<< obj.title << endl
				<< obj.author << endl
				<< obj.date << endl
				<< endl;
			cout<<"\nWas this the Title you wanted to remove?\n"
				<<"(y or n): ";
			cin >> choice;
			switch(choice)
			{
			case 'y':
				Remove_book(obj);
				return;
			case 'n':
				Clear_screen();
				break;
			default:
				Error("Invalid Option");
			}
			if(choice != 'y' && choice != 'n') Error("Invalid option");
		}
		if(search.eof()) break;
	}
	cout<<"\nPress any key to return to menu..."<<endl;
	cin.sync();
	cin.get();
	Clear_screen();
}

void Book::Remove_book(Node &obj)
{
	string author = " ";
	string title = " ";
	string date = " ";
	string c_space = " ";
	ifstream find("SF_Catalog.doc");
	ofstream temp("Temp.doc", ios::ate);
	do
	{
		getline(find, title);
		if(title.size() == 0) break;
		getline(find, author);
		if(author.size() == 0) break;
		getline(find, date);
		getline(find, c_space);
		if(date == obj.date && author == obj.author && title == obj.title)
		{
			cout<<"\nTitle removed."<<endl;
			cin.get();
		}
		else
		{	
			temp << title <<endl
			<< author << endl
			<< date << endl
			<< c_space <<endl;
		}
	}while(!(find.eof()));
	system("copy Temp.doc SF_Catalog.doc");
	temp.close();
	system("del Temp.doc");
	cout<<"\nPress any key to return to menu..."<<endl;
	cin.sync();
	cin.get();
	Clear_screen();
}




void Book::Cat_help()
{
	Clear_screen();
	cout<<"--Help--"<<endl;
	cout<<"\nIf this is your first time using this program, \n"
		<<"here are some tips you might find useful: \n"
		<<"\n1. Wherever you have this program saved, \n"
		<<"your catalog will appear in the same location.\n"
		<<"2. Your catalog will appear after you make your \n"
		<<"first entry.\n"
		<<"3. Navigate the menu by the number associated \n"
		<<"with that option.\n"
		<<"4. Entries are made as Title: Author: Date:."
		<<endl;
	cout<<"\nPress any key to return to menu..."<<endl;
	cin.ignore();
	cin.get();
	Clear_screen();
}



int main()
{
	cout<<"Welcome to the Sci-Fi cataloging program."<<endl;
	cout<<endl;
	cout<<"\nPlease enter your password: ";
	while(!(cin>>passtry))
	{
		Error("Invalid password.");
	}
	if(passtry != password) Error("Incorrect Password.");
	cout<<endl;
	cout<<"\nWelcome Sci-fi reader!"<<endl;
	cout<<"If this is your first time, choose Help."<<endl;
	cout<<"\nPlease press enter to view cataloging mode..."<<endl;
	cin.ignore();
	cin.get();

	Clear_screen();

	cout<<"(Please select an option by it's number.)"<<endl;

	do
	{
		cin.sync();
		Show_catalog();
		cout<<"Navigate to: ";
		if(!(cin>>choice)) Error("Error: Unknown option.");
		Book obj(choice);

	}while(choice != 6);

	cout<<"\nThank you for using our program, Goodbye."<<endl;
	cout<<endl;
	cin.ignore();
	cin.get();

	return 0;

}



includes misc.h
#include<iostream>
#include<string>
#include<fstream>
#include<vector>
#include"stdio.h"

using namespace std;

namespace Variables
{
	string password = "asimov";
	string passtry = " ";
	int choice = 0;
}


namespace Functions
{
	int Error(string s)
	{
		cout<< s << ", Goodbye.\n" <<endl;
		cin.sync();
		cin.get();
		exit(1);
	}

	void Clear_screen()
	{
		system("CLS");
	}

	void Show_catalog()
	{
		cout<<"--Catalog Menu--"<<endl;
		cout<<"    1. Search\n" 
			<<"    2. View all\n" 
			<<"    3. Add to catalog\n" 
			<<"    4. Remove from catalog\n" 
			<<"    5. Help\n"
			<<"    6. Quit\n" <<endl;
	}
}



Attached File  SF_Catalog.rtf (79.48K)
Number of downloads: 122
Attached File  includes misc.rtf (1.43K)
Number of downloads: 118

1 Comments On This Entry

Page 1 of 1

alias120 Icon

11 October 2010 - 06:52 PM
Alright, i'm on version two of this program. I cleaned up the code quite a bit, and created a seperate class for each menu option. Each class inherits from a base class, which provides the interface for main(). This provides better encapsulation of each option, and a more modular program. I also improved the Remove from Catalog option, fixing a bug where the program would fail to copy the books not removed under certain circumstances. I've included the source below, feel free to test it out and let me know what you think. Enjoy and Happy coding!

main.cpp
#include"interface.h"

int main()
{
	int choice = 0;
	cout<<"Welcome to the Sci-Fi cataloging program."<<endl;
	cout<<endl;
	enter_password();
	cout<<"\nPlease press enter to view cataloging mode..."<<endl;
	cin.get();
	while(true)
	{
		Show_catalog();
		if(!Accept_Input()) return false;
	}
	return 0;
}

void Show_catalog()
{
	Clear_screen();
	cout<<"--Catalog Menu--"<<endl;
	cout<<"    1. Search\n" 
		<<"    2. View all\n" 
		<<"    3. Add to catalog\n" 
		<<"    4. Remove from catalog\n" 
		<<"    5. Help (If first time navigate here)\n"
		<<"    6. Quit\n" <<endl;
}
	
bool Accept_Input()
{
	Book obj;
	Cat_Search obj2;
	Cat_View obj3;
	Cat_Add_To obj4;
	Cat_Remove obj5;
	int choice = 0;
	cout<<"Navigate to: ";
	if(!(cin>>choice)) Return_Error("\nUnknown option");
	switch(choice)
	{
	case 1:
		obj.nav_catalog(obj2);
		return true;
		break;
	case 2:
		obj.nav_catalog(obj3);
		return true;
		break;
	case 3:
		obj.nav_catalog(obj4);
		return true;
		break;
	case 4:
		obj.nav_catalog(obj5);
		return true;
		break;
	case 5:
		Cat_help();
		return true;
		break;
	case 6:
		cout<<"\nThank you for using our program, Goodbye."<<endl;
		cout<<endl;
		cin.ignore();
		cin.get();
		return false;
		break;
	default:
		Return_Error("\nUnknown option");
		return true;
	}
}



interface.h
#ifndef INTERFACE_H
#define INTERFACE_H
#include"includes.h"

class Node
{
	string author;
	string title;
	string date;
	friend class Cat_Search;
	friend class Cat_Remove;
};

class Book
{
protected:
	string t_search;
	string c_space;
	string title;
	string author;
	string file_read;
	int date;
	virtual void init()
	{
		return;
	}
public:
	Book(){}
	~Book(){}
	void nav_catalog(Book &obj)
	{
		obj.init();
	}
};

class Cat_Search : public Book
{
private:
	Node obj;
	int nav_choice;
public:
	Cat_Search(){}
	~Cat_Search(){}
protected:
	void init()
	{
		cin.sync();
		Clear_screen();
		cout<<"--Search--"<<endl;
		cout<<"\nYou can either search by Title, or Author."<<endl;
		cout<<"1. Title \n"
			<<"2. Author "<<endl
			<<"3. Main Menu"<<endl;
		cout<<"\nChoice: "; 
		if(!(cin>>nav_choice)) 
		{
			Return_Error("Incorrect Choice");
			return;
		}
		switch(nav_choice)
		{
		case 1:
			Title_Search(); 
			break;
		case 2:
			Author_Search();
			break;
		case 3:
			return;
			break;
		default:
			Return_Error("Unknown Option");
		}
	}

	void Title_Search()
	{
		Clear_screen();
		cin.sync();
		cout<<"\nPlease enter the Title you are searching for: ";
		getline(cin, t_search);
		ifstream search("SF_Catalog.doc");
		if(!File_Error_Check_in(search)) return;
		while(search.is_open())
		{	
			getline(search, obj.title);
			getline(search, obj.author);
			getline(search, obj.date);
			getline(search, c_space);

			if(obj.title == t_search)
			{
				cout<<"\nFound match: \n\n"
					<< obj.title << endl
					<< obj.author << endl
					<< obj.date << endl
					<< endl;
			}
			if(search.eof()) break;
		}
		cout<<"\nPress any key to return to menu..."<<endl;
		cin.get();
		Clear_screen();
	}

	void Author_Search()
	{
		Clear_screen();
		cin.sync();
		cout<<"\nPlease enter the Author you are searching for: ";
		getline(cin, t_search);
		ifstream search("SF_Catalog.doc");
		if(!File_Error_Check_in(search)) return;
		do
		{	
			getline(search, title);
			if(title.size() == 0) break;
			obj.title = title;
			getline(search, author);
			if(author.size() == 0) break;
			obj.author = author;
			getline(search, obj.date);
			getline(search, c_space);

			if(obj.author == t_search)
			{
				cout<<"\nFound match: \n\n"
					<< obj.title << endl
					<< obj.author << endl
					<< obj.date << endl
					<< endl;
			}
		}while(!(search.eof()));
		cout<<"\nPress any key to return to menu..."<<endl;
		cin.get();
		Clear_screen();
	}
};
	
class Cat_View : public Book
{
public:
	Cat_View(){}
	~Cat_View(){}
protected:
	void init()
	{
		Clear_screen();
		cout<<"--Full Catalog--\n"<<endl;
		ifstream obj("SF_Catalog.doc",ios::out);
		if(!File_Error_Check_in(obj)) return;
		while(obj.is_open())
		{
			getline(obj, file_read);
			cout<< file_read <<endl;
			if(obj.eof()) break;
		}
	
		cout<<"\nPress any key to return to menu..."<<endl;
		cin.ignore();
		cin.get();
	}
};

class Cat_Add_To : public Book
{
private:
	int choice;
public:
	Cat_Add_To(){}
	~Cat_Add_To(){}
protected:
	void init()
	{
		cin.sync();
		Clear_screen();
		cout<<"--Add Entry--"<<endl;
		cout<<"\n1. Add Entry to Catalog \n"
			<<"2. Main Menu "<<endl;
		cout<<"\nChoice: ";
		if(!(cin>>choice)) 
		{
			Return_Error("Invalid Input");
			return;
		}
		switch(choice)
		{
		case 1:
			Add_To();
			break;
		case 2:
			break;
		default:
			Return_Error("Invalid Choice");
		}
	}

	void Add_To()
	{
		cin.sync();
		Clear_screen();
		cout<<"--Add Entry--"<<endl;
		cout<<"\nEnter the Title: ";
		getline(cin,title);
		cout<<"\nEnter the Author: ";
		getline(cin,author);
		cout<<"\nEnter the Publication Date: ";
		if(!(cin>>date)) 
		{
			Return_Error("Invalid Input");
			return;
		}
		ofstream obj("SF_Catalog.doc",::ios::app);     //Change to append or ate file
		if(!File_Error_Check_out(obj)) return;
		obj << title <<endl
			<< author << endl
			<< date << endl
			<< endl;
		cout<<"\nEntry made."<<endl;
		cout<<"\nPress any key to return to menu..."<<endl;
		cin.ignore();
		cin.get();
	}
};

class Cat_Remove : public Book
{
private:
	string r_search;
	string c_space;
	string t_title;
	string t_author;
	char choice;
	int choose;
	Node obj;
public:
	Cat_Remove(){}
	~Cat_Remove(){}
protected:
	
	void init()
	{
		Clear_screen();
		cin.sync();
		cout<<"--Remove from Catalog--\n"<<endl;
		cout<<"Would you like to search for a Title, or "
			<<"an Author to remove?\n"<<endl;
		cout<<"1. Title \n"
			<<"2. Author \n"
			<<"3. Main Menu \n"<<endl;
		cout<<"Choice: ";
		if(!(cin>>choose)) 
		{
			Return_Error("Incorrect Choice");
			return;
		}
		switch(choose)
		{
		case 1:
			Title_Input();
			break;
		case 2:
			Author_Input();
			break;
		case 3:
			break;
		default:
			Return_Error("Invalid Option");
			break;
		}
	}

	void Title_Input()
	{
		cin.sync();
		cout<<"\nTitle to Remove: ";
		getline(cin, r_search);
		Title_Remove(r_search);
	}
	void Author_Input()
	{
		cin.sync();
		cout<<"\nAuthor to search for: ";
		getline(cin, r_search);
		Author_Remove(r_search);
	}

	void Title_Remove(string r_search)
	{
		ifstream search("SF_Catalog.doc");
		if(!File_Error_Check_in(search)) return;
		ofstream temp("Temp.doc", ios::app);
		if(!File_Error_Check_out(temp)) return;
		while(search.is_open())
		{	
			Clear_screen();
			getline(search, obj.title);
			t_title = obj.title;
			if(t_title.size() == 0) break;
			getline(search, obj.author);
			//t_author = obj.author;
			//if(t_author.size() == 0) break;
			getline(search, obj.date);
			getline(search, c_space);

			if(obj.title == r_search)
			{
				cout<<"\nLooking...\n"<<endl;
				cout<<"\nFound match: \n\n"
					<< obj.title << endl
					<< obj.author << endl
					<< obj.date << endl
					<< endl;
				cout<<"\nWas this the Book you wanted to remove?\n"
					<<"(y or n): ";
				cin >> choice;
				switch(choice)
				{
				case 'y':
					Remove_book(obj);
					break;
				case 'n':
					No_Remove(obj, temp);
					break;
				default:
					Error("Invalid Option");
				}
				if(choice != 'y' && choice != 'n') Error("Invalid option");
			}
			else No_Remove(obj, temp);
			if(search.eof()) search.close();
		}
		system("copy Temp.doc SF_Catalog.doc");
		temp.close();
		system("del Temp.doc");
		Clear_screen();
		cout<<"\nComplete."<<endl;
		cout<<"\nPress any key to return to menu..."<<endl;
		cin.get();
	}

	void Author_Remove(string r_search)
	{
		ifstream search("SF_Catalog.doc");
		if(!File_Error_Check_in(search)) return;
		ofstream temp("Temp.doc", ios::app);
		if(!File_Error_Check_out(temp)) return;
		while(search.is_open())
		{	
			Clear_screen();
			getline(search, obj.title);
			t_title = obj.title;
			if(t_title.size() == 0) break;
			getline(search, obj.author);
			t_author = obj.author;
			if(t_author.size() == 0) break;
			getline(search, obj.date);
			getline(search, c_space);

			if(obj.author == r_search)
			{
				cout<<"\nLooking...\n"<<endl;
				cout<<"\nFound match: \n\n"
					<< obj.title << endl
					<< obj.author << endl
					<< obj.date << endl
					<< endl;
				cout<<"\nWas this the Book you wanted to remove?\n"
					<<"(y or n): ";
				cin >> choice;
				switch(choice)
				{
				case 'y':
					Remove_book(obj);
					break;
				case 'n':
					No_Remove(obj, temp);
					break;
				default:
					Error("Invalid Option");
				}
				if(choice != 'y' && choice != 'n') Error("Invalid option");
			}
			else No_Remove(obj, temp);
			if(search.eof()) search.close();
		}
		system("copy Temp.doc SF_Catalog.doc");
		temp.close();
		system("del Temp.doc");
		Clear_screen();
		cout<<"\nComplete."<<endl;
		cout<<"\nPress any key to return to menu..."<<endl;
		cin.get();
	}

	void Remove_book(Node &obj)
	{
		cin.sync();
		cout<<"\n"<< obj.title <<" removed."<<endl;
	}

	void No_Remove(Node &obj, ofstream &temp)
	{
		cin.sync();
		if(temp.is_open())
		{
			temp << obj.title <<endl
			<< obj.author << endl
			<< obj.date << endl
			<< c_space <<endl;
		}
		else Return_Error("Error writing to File.");
	}

};

void Cat_help()
{
	Clear_screen();
	cout<<"--Help--"<<endl;
	cout<<"\nIf this is your first time using this program, \n"
		<<"here are some tips you might find useful: \n"
		<<"\n1. Wherever you have this program saved, \n"
		<<"your catalog will appear in the same location.\n"
		<<"2. Your catalog will appear after you make your \n"
		<<"first entry.\n"
		<<"3. Navigate the menu by the number associated \n"
		<<"with that option.\n"
		<<"4. Entries are made as Title: Author: Date:."
		<<endl;
	cout<<"\nPress any key to return to menu..."<<endl;
	cin.ignore();
	cin.get();
}
#endif



includes.h
#ifndef INCLUDES_H
#define INCLUDES_H
#include<iostream>
#include<string>
#include<fstream>
#include<vector>
#include <sys/stat.h>

using namespace std;

string filename = "SF_Catalog.doc";
string password = "asimov";
string passtry = " ";
struct stat statInfo;
extern void Show_catalog();
extern bool Accept_Input();
void enter_password();
bool File_Test();

bool File_Error_Check_in(ifstream &obj)
{
	if(!obj)
	{
		cout<<"No file in directory."<<endl;
		cout<<"\nPress any key to return to main menu..."<<endl;
		cin.ignore();
		cin.get();
		return false;
	}
	else return true;
}

bool File_Error_Check_out(ofstream &obj)
{
	if(!obj)
	{
		cout<<"No file in directory."<<endl;
		cout<<"\nPress any key to return to main menu..."<<endl;
		cin.ignore();
		cin.get();
		return false;
	}
	else return true;
}

int Error(string s)
{
	cout<< s << ", Goodbye.\n" <<endl;
	cin.sync();
	cin.clear();
	cin.get();
	exit(1);
}

void Return_Error(string s)
{
	cout<< s << ", press enter to return to the main menu.\n"<<endl;
	cin.sync();
	cin.clear();
	cin.get();
}

void password_error(int i)
{
	static int time_out = 0;
	if(i == 1)
	{
		cout<<"You did not enter your password correctly, please try again."<<endl;
		enter_password();
	}
	if(i == 2)
	{
		cout<< "\nIncorrect Password.\n"<<endl;
		time_out++;
		if(time_out == 3) Error("You have failed too many password attempts");
		enter_password();
	}
}

bool File_Test()
{
	int statTest = 0;
	statTest = stat(filename.c_str(), &statInfo);
	if(statTest == 0) return true;
	else return false;
}

void Clear_screen()
{
	system("CLS");
}

void enter_password()
{
	if(File_Test()) return;
	cout<<"\nPlease enter your password: ";
	while(!(cin>>passtry))
	{
		password_error(1);
	}
	if(passtry != password) password_error(2);
	else
		cout<<"\nWelcome Sci-fi reader!"<<endl;
		cout<<"Since this is your first time, choose Help from the main menu."<<endl;
		cin.sync();
}
#endif

0
Page 1 of 1

Trackbacks for this entry [ Trackback URL ]

There are no Trackbacks for this entry

October 2014

S M T W T F S
   1234
567891011
12131415161718
19202122232425
26272829 30 31 

Tags

Recent Entries

Recent Comments

Search My Blog

1 user(s) viewing

1 Guests
0 member(s)
0 anonymous member(s)