Main routine


// flash.cpp : Defines the entry point for the console application.
/********************************************
 * auther: Kwun Fong (Peter)
 * data:   5/25/2005
 * class:  C/C++ Programming IV
 *
 * flash.cpp
 *
 *    Main driver to the usage of the Flashcard 
 *    collection.
 *
 */
#include 
#include  // random_shuffle
#include   // iterator
#include  // cout, cin, endl

#include "Flashcards.h"
#include "Word.h"

using namespace std;

int main( /* int argc, char* argv[] */ ) {

	char choice;
	string filename;
	bool quit = false;

	Flashcards cards;

	do {
		cout << "Main Menu:" << endl;
		cout << "**********" << endl;
		cout << "  1) Import flashcards" << endl;
		cout << "  2) Export flashcards" << endl;
		cout << "  3) Clear" << endl;
		cout << "  4) Study" << endl;
		cout << "Please enter your selection ('q' to quit): ";
		cin  >> choice;
		
		switch( choice ) {
			case 'q':
				quit = true;
				break;

			case '1':
				cout << "Enter a filename: ";
				cin >> filename;
				cout << "Loading " << filename << "... ";
				try {
					cards.import( filename );
				} catch( char* errmsg ) {
					cout << "FAILED - " << errmsg << endl << endl;
					continue;
				}
				cout << "SUCCESS" << endl;
				break;

			case '2':
				cout << "Enter a filename: ";
				cin >> filename;
				cout << "Export to " << filename << "...";
				if( !cards.empty() ) {
					try {
						cards.export( filename );
					} catch( char* errmsg ) {
						cout << "FAILED - " << errmsg << endl;
						continue;
					}
					cout << "SUCCESS" << endl;
				} else { 
					cout << "FAILED - ";
					cout << "No flashcards have been loaded yet." << endl;
				}

				break;

			case '3':
				cout << "Clear flashcards...SUCCESS" << endl;
				cards.clear();
				break;
	
			case '4':
				if( !cards.empty() ) {
					cout << "study mode" << endl;
					random_shuffle( cards.begin(), cards.end() );
					Flashcards::iterator itr = cards.begin();
					while(itr != cards.end()) {
						cout << endl;
						cout << (**itr).getName() << endl;
						cout << "'n' for next & 'd' for definition: ";
						cin >> choice;
						switch( choice ) {
							case 'n':
								//do nothing
								//fall through to the next one word
								break;

							case 'd':
							{
								cout << (**itr).getPartOfSpeech() << ". "
									 << (**itr).getDefinition() << endl;
								vector examples = (**itr).getExamples();
								vector::iterator examples_itr = examples.begin();
								while( examples_itr != examples.end() ) {
									cout << " - " << (*examples_itr) << endl;
									examples_itr++;
								}
								break;
							}
							default:
								// do nothing
								break;
						}
						itr++;
					}
				} else {
					cout << "No flashcards have been loaded yet." << endl;
				}
				break;

			default:
				cout << "Invalid command.  Retry again." << endl;
				break;
		}
		cout << endl;
	} while( !quit );
	return 0;
}

Word Class Declared


#ifndef WORD
#define WORD
/********************************************
 * auther: Kwun Fong (Peter)
 * data:   5/25/2005
 * class:  C/C++ Programming IV
 *
 * Word.h
 *
 *    Data storage class for a word
 *
 */

#include 
#include 
#include 
#include  // ostream, istream; cout, cin, endl;

using namespace std;

class Word {
	friend ostream& operator<<(ostream& os, const Word& aWord);
	friend istream& operator>>(istream& is, Word& aWord);
public:
	// constructor
	Word( string word, string part_of_speech, string definition );
	// destructor
	~Word() {};
	// adds example usage to the word
	void addExample( string example );

	// the word
	string getName() const;
	// part of speech (ie v, n, abj, etc)
	string getPartOfSpeech() const;
	// definition of the word
	string getDefinition() const;
	// example usage of the word
	vector getExamples() const;

private:
	string         name;
	string         part_of_speech;
	string         definition;
	vector examples;
};

#endif

Word Class Code


/********************************************
 * auther: Kwun Fong (Peter)
 * data:   5/25/2005
 * class:  C/C++ Programming IV
 *
 * Word.cpp
 *
 *    Data storage class for a word
 *
 */
#include "Word.h"

Word::Word( string name, string part_of_speech, string definition ) {
	this->name = name;
	this->part_of_speech = part_of_speech;
	this->definition = definition;
}


void Word::addExample( string example ) {
	this->examples.push_back( example );
}


string Word::getName() const {
	return this->name;
}


string Word::getPartOfSpeech() const {
	return this->part_of_speech;
}


string Word::getDefinition() const {
	return this->definition;
}


vector Word::getExamples() const {
	return this->examples;
}


ostream& operator<<(ostream& os, const Word& aWord)
{
	os << aWord.getName() << " " << aWord.getPartOfSpeech() << endl;
	os << "  " << aWord.getDefinition() << endl << endl;
	vector::iterator itr = aWord.getExamples().begin();
	vector examples = aWord.getExamples();
	for(int index = 0; index < aWord.getExamples().size(); index++) {
		os << "  -" << examples[index] << endl;
	}

    return os;
}


istream& operator>>(istream& is, Word& aWord) {
	string name, part_of_speech, definition;

	std::getline( is, name );
	std::getline( is, part_of_speech );
	std::getline( is, definition );
	cout << name << "; " << part_of_speech << "; " << definition << endl;

	return is;
}

Flashcards Class Declared


#ifndef FLASHCARDS
#define FLASHCARDS
/********************************************
 * auther: Kwun Fong (Peter)
 * data:   5/25/2005
 * class:  C/C++ Programming IV
 *
 * Flashcards.h
 *
 *    a collection of Words
 *
 */

#include 
#include 
#include 
#include 
#include "Word.h"
using namespace std;

class Flashcards : public vector {
	friend ostream& operator<<( ostream& os, const Flashcards& cards );
public:
	// constructor
	Flashcards() {};
	Flashcards( string filename ); 
	// destructor
	~Flashcards();

	// import flashcards
	void import( string filename );
	// export flashcards to a file
	void export( string filename );

	// override clear to depth delete each elements
	void clear();
};

#endif

Flashcards Class Code


/********************************************
 * auther: Kwun Fong (Peter)
 * data:   5/25/2005
 * class:  C/C++ Programming IV
 *
 * Flashcards.h
 *
 *    a collection of Words
 *
 */

#include "Flashcards.h"

Flashcards::Flashcards( string filename ) {
	this->import( filename );
}


Flashcards::~Flashcards() {
	this->clear();
}


void Flashcards::import( string filename ) {
	ifstream in_file( filename.c_str() );	
	if ( !in_file ) 
	{
		throw "unable to open file input";
	}

	string name;
	string part_of_speech;
	string definition;

	int counter = 0;
	string data;
	Word* aWord;
	while( std::getline( in_file, data ) ) {
		if( data == "" ) {
			counter = 0; // start of a new word
			continue;
		}

		switch( counter++ ) {
			case 0:
				name = data;
				break;
			case 1:
				part_of_speech = data;
				break;
			case 2:
				definition = data;
				aWord = new Word(name, part_of_speech, definition);
				this->push_back(aWord);
				break;
			default:
				aWord->addExample( data );
				break;
		}
	}
}


void Flashcards::export( string filename ) {
	ofstream out_file( filename.c_str() );
	if ( !out_file ) {
		throw "unable to open file for output";
	}

	Flashcards::iterator itr = this->begin();
	while( itr != this->end() ) {
		out_file << (**itr).getName() << endl;
		out_file << (**itr).getPartOfSpeech() << endl;
		out_file << (**itr).getDefinition() << endl;
		vector examples = (**itr).getExamples();
		vector::iterator examples_itr = examples.begin();
		while( examples_itr != examples.end() ) {
			out_file << *examples_itr << endl;
			examples_itr++;
		}
		out_file << endl;
		itr++;
	}
}


void Flashcards::clear() {
	for(int index = 0; index < this->size(); index++) {
		delete at(index);
	}
	vector::clear();
}


ostream& operator<<( ostream& os, const Flashcards& cards ) {
	os << "Your flashcards contain: " << endl;
	os << "************************ " << endl;
	for(int index = 0; index < cards.size(); index++) {
		os << *(cards.at(index)) << endl;
	}
    return os;
}

Flashcards Data File


abate
v
to lessen in intensity or degree
We realized with great relief that the storm had abated before breaking through the sea.

aberrant
adj
deviating from the norm

abjure
v
to renounce or reject solemnly; to recant; to avoid
The reformed socialite abjured her former lifestyle and all those with whom she had previously associated.
Steve had to abjure all indulgence when he entered the training camp.

abrogate
v
to abolish or annul by authority; put down

abscission
n
act of cutting off or removing
Dr. Carther recommended an immediate abscission of the abscess in order to minimize any further infection.

abscond
v
to depart clandestinely; to steal off and hide
Doug was left penniless when the two con men absconded with his life savings.
Hosted by www.Geocities.ws

1