Tilted Forum Project Discussion Community  

Go Back   Tilted Forum Project Discussion Community > Interests > Tilted Technology


 
 
LinkBack Thread Tools
Old 12-10-2007, 06:20 PM   #1 (permalink)
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  
Old 12-11-2007, 05:34 AM   #2 (permalink)
Psycho
 
connyosis's Avatar
 
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..
connyosis is offline  
Old 12-11-2007, 07:44 AM   #3 (permalink)
Junkie
 
zero2's Avatar
 
thank you connyosis for your help.
zero2 is offline  
Old 12-11-2007, 09:16 AM   #4 (permalink)
Psycho
 
connyosis's Avatar
 
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.
connyosis is offline  
 

Tags
address, book


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 05:33 AM.

Tilted Forum Project

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project

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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360