View Single Post
Old 12-10-2007, 06:20 PM   #1 (permalink)
zero2
Junkie
 
zero2's Avatar
 
[c++] Address Book

I'm trying to create a fairly simple address book, and everything works until I try to search for a contact in my address book. I decided not to code inputing the address and telephone number until I have the program working properly.

When I select the option to find contacts, something weird happens.

For instance, if 1 record has john doe, and another has jane doe, and I try to find all people w/ last name doe, what prints out is:

doe doe

jane doe

when it should be:

john doe
jane doe

Code:
/* Program name: address_book.cpp
*  Date: 2007.12.10
*
*/

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

void simpleMenu(); 


class person
{
public:
	string first;
	string last;
};
const int NUM = 2; // Sets # of contacts in Address Book
/* - - - - - - - - - -- - - - - - - - - - MAIN() - - - - - - - - - -- - - - - - - - - - */
void main(void)
{
	simpleMenu(); // Executes simpleMenu function
}

/* Function name: simpleMenu()
*  Description: Display menu. Allows users to add, 
                view, find contacts, and exit program.
*/

void simpleMenu()
{
	/* Variables */
	/* ===================================== */
	int iChoice; 
	person add[NUM];
	string find[1]; // used to find person in Address Book
	int i; 
	int iSize;
	int iCheck = 0;// checks whether or not data/contact exists
	int noMatch = 0;// used to check if match or not. 0 = match, 1 = no match :D

	iSize = NUM;


/* - - - - - - - - - -- - - - - - - - - - MENU - - - - - - - - - -- - - - - - - - - - */

	do
	{
	cout << "Welcome to Address Book\n" << endl;

	cout << "==========================================\n";
	cout << "1. Add Contacts to Address Book\n";
	cout << "2. View Contacts in Address Book\n";
	cout << "3. Find Contacts in Address Book\n";
	cout << "4. Exit Address Book\n\n";
	cout << "==========================================\n";

	cout << "Please enter choice (1 - 4): ";
	cin >> iChoice;

	switch (iChoice)
	{
/* - - - - - - - - - -- - - - - - - - - - ADD CONTACTS - - - - - - - - - -- - - - - - - - - - */
	case 1: 
		cout << "\nAdd Contacts -- \n" << endl; // Adding Contacts

		for (i = 0; i < iSize; i++)
		{
			cout << "Read and enter the following information to add a contact.\n";
			cout << "----------------------------------------------------------\n";
			cout << "Enter First Name: ";
			cin >> add[i].first;
			cout << "\nEnter Last Name ";
			cin >> add[i].last;
			cout << "\n" << "Contact successfully added to Address Book!\n\n";
			iCheck ++;
		}
		break;
/* - - - - - - - - - -- - - - - - - - - - VIEW CONTACTS - - - - - - - - - -- - - - - - - - - - */
	case 2: 
		if (iCheck != 0) // Checks to see if contacts have been added!
		{
			cout << "View Contacts -- " << endl;
			for (i = 0; i < iSize; i++)
			{
				cout << add[i].first << " " << add[i].last << "\n" << endl;
			}
		}
		else
		{
			cout << "There are no contacts to view!\n"; // If no contacts are added
		}                                               // selection is disabled.
		break;
/* - - - - - - - - - -- - - - - - - - - - FIND CONTACTS - - - - - - - - - -- - - - - - - - - - */
	case 3: 
		if (iCheck != 0) // Checks to see if contacts have been added!
		{
		cout << "\nFind Contacts -- " << endl;
		
		cout << "Enter last name: ";
		cin >> find[1];

		for (i = 0; i < iSize; i++)
		{
			if (add[i].last == find[1])
			{
				cout << add[i].first << " " << add[i].last << "\n";
				noMatch = 0; // if match, set noMatch to zero.
			}
			else
			{
				noMatch = 1;//if match is not found set noMatch to one.
			}
		}
		if (noMatch == 1)// if noMatch is one, print No match found.
		{
			cout << "No match found\n";
		}
		else
		{
			cout << "There are no contacts to find!\n"; // If no contacts are added
			                                            // selection is disabled.
		} 
		break;
/* - - - - - - - - - -- - - - - - - - - - EXIT PROGRAM - - - - - - - - - -- - - - - - - - - */
	case 4:
		cout << "\nExit Program -- " << endl;
		break;
/* - - - - - - - - - -- - - - - - - - - - TRY AGAIN - - - - - - - - - -- - - - - - - - - - */
	default: 
		cout << "\nTry Again!\n" << endl;
		break;
	}
	}
	while (iChoice != 4);

	return;
}
zero2 is offline  
 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73