0

This is a program my instructor wrote and I have to create a program that similar in format. My issue is I don't quite understand what main is doing or how it works, was hoping someone could explain it to me in detail.

Rolodex.h

#ifndef _ROLODEX_H_
#define _ROLODEX_H_

#include "card.h"
#include <iostream>
#include <fstream>
#include <stdlib.h>
using namespace std;

class rolodex
{
	private:
		fstream	file;

	public:
			rolodex()	// constructor opens the file
			{
				// input and output stream

				file.open("rolodex.txt", ios::in | ios::out | ios::app);
				if (!file.good())
				{
				      cerr << "ERROR: unable to open \"rolodex.txt\"\n";
				      exit(1);
				}

			}

			~rolodex()	// destructor flushes and closes the file
			{
				file.flush();
				file.close();
			}

		void interactive();
		char menu();
		void search();
		void search(char* name);
		void enter();
		void enter(char* name, char* address, char* phone);
		int read(card deck[]);
		void print(card* entry) { entry->print(); }
		void list();
		int sort(card* list);
};

#endif

Card.h

#ifndef _CARD_H_
#define _CARD_H_

/*
 * An instance of the card class represents one card in a rolodex file.
 * Each card contains the contact information for one person as a set of three c-strings:
 * name, address, and phone number.
 */

// prevent warning about Microsoft deprecated c-string functions

#define _CRT_SECURE_NO_DEPRECATE

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

enum sizes { NAME_SZ = 20, ADDR_SZ = 40, PHONE_SZ = 15 };	// the sizes of card fields

class card
{
	private:	
		char	name[NAME_SZ];
		char	address[ADDR_SZ];
		char	phone[PHONE_SZ];

	public:
			card()
			{
				name[0] ='\0';
				address[0] ='\0';
				phone[0] ='\0';
			}

			card(char* n, char* a, char* p)
			{
				strcpy(name, n);
				strcpy(address, a);
				strcpy(phone, n);
			}

			card(char* line)
			{
				strcpy(name, strtok(line, ":"));
				strcpy(address, strtok(NULL, ":"));
				strcpy(phone, strtok(NULL, ":"));
			}

		void print()
		{
			cout.setf(ios::left);

			cout << setw(NAME_SZ) << name;
			cout << setw(ADDR_SZ) << address;
			cout << setw(PHONE_SZ) << phone << endl;
		}

		friend int card_comp(const void* c1, const void* c2);
};

#endif

Rolodex.cpp

#define _CRT_SECURE_NO_DEPRECATE

#include "card.h"
#include "rolodex.h"
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;

int main(int argc, char* argv[])
{
	rolodex	contacts;

	if (argc == 1)			// no arguments ==> enter interactive mode
		contacts.interactive();

	else if (argc == 2 && !strcmp(argv[1], "-l"))
		contacts.list();

	else if (argc == 2)
		contacts.search(argv[1]);	// 1 arg ==> search for arg and quit

	else if (argc == 4)
		contacts.enter(argv[1], argv[2], argv[3]); // 3 args > new entry & quit

	else
		cerr << "USAGE: rolodex | rolodex -l | rolodex <name> | rolodex <name> <address> <phone>\n";

	return 0;
}
// prints a short menu and reads the user's choice

char rolodex::menu()
{
	system("cls");

	cout << "S\t Search For A Name \n";
	cout << "E\t Enter A New Name \n";
	cout << "L\t List All Entries \n";
	cout << "Q\t Quit \n";

	cout << "\n\nCommand: ";

	char command;
	cin >> command;
	cin.ignore();						// discard the new line

	return command;
}
// interactive mode displays a menu and does what the user want until the program ends

void rolodex::interactive()
{
	while (true)
	{
		char operation = menu();

		switch (operation)
		{
			case 's' :				// search
			case 'S' :
				search();
				system("pause");
				break;

			case 'e' :				// enter
			case 'E' :
				enter();
				break;

			case 'l' :				// list
			case 'L' :
				list();
				system("pause");
				break;

			case 'q' :				// quit
			case 'Q' :
				exit(0);

			default:
				cerr << "Unknown option: " << operation << endl;
				break;
		}

	}

}

// prompts for a search name and searches

void rolodex::search()
{
	char name[NAME_SZ];

	cout << "Please enter the search name: ";
	cin.getline(name, NAME_SZ);
	search(name);
}
// searches for the given name

void rolodex::search(char* name)
{
	card list[50];
	int	count = sort(list);
	card key(name, "", "");

	card* entry = (card *)bsearch(&key, list, count, sizeof(card), card_comp);

	if (entry != NULL)
		print(entry);

	else
		cout << name << " was not found in the Rolodex\n";
}
// reads all data from the rolodex file in a list of rolodex cards

int rolodex::read(card deck[])
{
	file.clear();			// clear EOF -- MUST come before seek
	file.seekg(0);			// return to beginning of file

	int count = 0;
	char line[100];

	while (file.getline(line, 100))
	{
		deck[count] = card(line);
		count++;
	}

	return count;
}
// prompts for data to create a new entry in the rolodex file

void rolodex::enter()
{
	char name[NAME_SZ];
	char address[ADDR_SZ];
	char phone[PHONE_SZ];

	cout << "Please enter the new name: ";
		cin.getline(name, NAME_SZ);

	cout << "Please enter the new address: ";
		cin.getline(address, ADDR_SZ);

	cout << "Please enter the new phone #: ";
		cin.getline(phone, PHONE_SZ);

	enter(name, address, phone);
}
// enters a new card into the rolodex file

void rolodex::enter(char* name, char* address, char* phone)
{
	file.clear();			// clear EOF
	file.seekp(0, ios::end);	// move to end of file

	file << name << ":" << address << ":" << phone << endl;

	file.flush();
}
// lists all data in the rolodex file

void rolodex::list()
{
	card list[50];
	int	count = sort(list);

	for (int i = 0; i < count; i++)
		print(&list[i]);
}
// reads and sorts a list of rolodex cards

int rolodex::sort(card* list)
{
	int count = read(list);

	qsort(list, count, sizeof(card), card_comp);	// sorts a list of rolodex cards

	return count;
}
// alphabetically compares two rolodex cards
// returns < 0 if c1 comes before c2
// returns 0 if c1 == c2
// returns > 0 if c1 comes after c2

int card_comp(const void* c1, const void* c2)//compares the names on two rolodex cards
{
	return strcmp(((card *)c1)->name, ((card *)c2)->name);
}

Edited by rebellion346: n/a

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by Shantari
0

argc stands for Argument Count, which is the amount of arguments passed to the application and argv stands for Argument Values which is an array of the actual values passed to the application, with argv[0] always being the name of the application. They allow command line arguments to be passed to your program. As for the rest of your program I have not particularly read through it, but by your thread title I assume you wanted an explanation of the main() parameters?

Read more about Command Line Arguments on Wikipedia.

Edited by Shantari: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.