12-10-2007, 06:20 PM | #1 (permalink) |
Junkie
|
[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; } |
12-11-2007, 05:34 AM | #2 (permalink) |
Psycho
Location: Sweden - Land of the sodomite damned
|
What's with the string find[1]?
Replacing all occurances of find[1] with find, and the program works as expected.
__________________
If atheism is a religion, then not collecting stamps is a hobby. Last edited by connyosis; 12-11-2007 at 05:39 AM.. |
12-11-2007, 09:16 AM | #4 (permalink) |
Psycho
Location: Sweden - Land of the sodomite damned
|
I guess that when you do string find[1] you're creating an array of strings containing just one string, hence to access that you should use find[0] instead of find[1] since find[1] would be outside the array. (If I'm wrong, somebody correct me)
Of course, it's pretty useless to create an array with just one element instead of just using the string object directly. (That is, just use std::string name instead of std::string name[0])
__________________
If atheism is a religion, then not collecting stamps is a hobby. |
Tags |
address, book |
|
|