Address Book Program

I need help with this really urgent! : (

Page 1 of 1

6 Replies - 2888 Views - Last Post: 05 May 2008 - 10:37 PM Rate Topic: -----

#1 Zerobu   User is offline

  • Black Hatter

Reputation: 13
  • View blog
  • Posts: 1,822
  • Joined: 14-January 08

Address Book Program

Posted 05 May 2008 - 12:11 AM

So i am writing a program that is store addresses in files. I need help with with one of my functions


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

 /* globals */
const int PHONESIZE = 9;
const int FIRST_NAME = 15;
const int LAST_NAME = 15;
const int STREET = 30;
const int CITY = 20;
const int STATE = 3;
const int ZIP = 6;


struct AddressRecord
{
	char fname[FIRST_NAME];
	char lname[LAST_NAME];
	char phone[PHONESIZE];
	char street[STREET];
	char city[CITY];
	char state[STATE];
	char zip[ZIP];

};

void AddName(fstream &datafile, int &num_names)
{
	AddressRecord temp;
	int i;

	cin.get();

	cout << "\nLast name? ";
	cin.getline(temp.lname,FIRST_NAME);

	
	cout<<"First name? ";
	cin.getline(temp.fname, FIRST_NAME);


	cout <<"Street? ";
	cin.getline(temp.street, STREET);


	cout<<"City? ";
	cin.getline(temp.city, CITY);

	cout<< "State? ";
	cin.getline(temp.state, STATE);

	cout<<"Zip? ";
	cin.getline(temp.zip, ZIP);




	cout << "Phone number? ";
	cin.getline(temp.phone,PHONESIZE);


	/* update number of names at start of file */

	num_names++;
	datafile.seekp(0);
	datafile.write((char *) & num_names,sizeof(num_names));

	/* now store the name in the proper location */

	datafile.seekp(sizeof(num_names) + (num_names-1)*sizeof(AddressRecord));
	datafile.write((char *) &temp, sizeof(AddressRecord));
	
	for (  i= 0; i< num_names; i++)
	{
	}

	
	cout << endl << temp.fname<<" "<<temp.lname <<" added  as record "<<i;
}

void change_phone(fstream & datafile, int & num_names)
{
	AddressRecord temp;
	int record_num;

	do
	{
		cout << "\nRecord number to change? ";
		cin >> record_num;
		cout << endl;

		if(num_names < record_num || record_num <= 0)
			cout << "\nRecord  not found.  Please re-enter a valid record ";


	}while (num_names < record_num || record_num <= 0);


	datafile.seekg(sizeof(int) + sizeof(temp)*(record_num - 1));
	datafile.read( (char *) &temp, sizeof(temp) );

	
	cin.get();
	cout << "Last name: " << temp.lname << endl;
	cout << "Last name? ";
	cin.getline(temp.lname, LAST_NAME);
	if(temp.lname==NULL)
		cin.get();



	cout << "First name: "<<temp.fname <<endl;
	cout << "First name? ";
	cin.getline(temp.fname, FIRST_NAME);
	


	cout <<"Street: " << temp.street <<endl;
	cout <<"Street? ";
	cin.getline(temp.street, STREET);

	

	cout <<"City: "<< temp.city<<endl;
	cout << "City? ";
	cin.getline(temp.city, CITY);
	
	
	cout <<"State: "<<temp.state<<endl;
	cout <<"State? ";
	cin.getline(temp.state, STATE);


	cout <<"Zip: "<<temp.zip<<endl;
	cout <<"Zip? ";
	cin.getline(temp.zip, ZIP);


	cout <<"Phone: " <<temp.phone<<endl;
	cout <<"Phone? ";
	cin.getline(temp.phone, PHONESIZE);

	
	
	datafile.seekp( sizeof(int) + sizeof(temp)*( record_num - 1) );
	datafile.write( (char *) & temp, sizeof( temp ) );

	cout<<"\nRecord "<<record_num<<" has been changed ";

	
}


void ListNames(fstream &datafile, int num_names)
{
	AddressRecord temp;
	int count;

	for (count=0; count < num_names; count++) 
	{
		datafile.seekg(sizeof(num_names) + count*sizeof(AddressRecord));
		datafile.read((char *) &temp, sizeof(AddressRecord));
		cout <<"\nRecord "<<count+1<<":"<<endl;
		cout  << temp.fname <<" "<<temp.lname<<endl;
		cout << temp.street <<endl;
		cout << temp.city<<", " << temp.state << " " << temp.zip << endl;
		cout<< temp.phone << endl;
	}
}

void SearchForName()
{
}

void main() 
{
	int num_names;
	char choice;
	ifstream datain;
	fstream datafile;

	datain.open("address.dat");	 // test file existence
	if (datain.fail())			  // if it does not exist, create it
	{		  
		datain.close();
		datafile.open("address.dat",ios::out|ios::binary);
		cout << "Initializing new file..." << endl;

		num_names = 0;
		datafile.seekp(0);
		datafile.write((char *) &num_names,sizeof(num_names));
		datafile.close();
		datafile.open("address.dat",ios::in|ios::out|ios::binary);
	}
	else 
	{						// it already exists
		datain.close();			// reopen as two way pathway
		datafile.open("address.dat",ios::in|ios::out|ios::binary);
		datafile.seekg(0);
		datafile.read((char *) &num_names,sizeof(num_names));
		cout << "There are presently " << num_names << " names." << endl;
	}


	// two way stream is open at this point, regardless
	do
	{

		cout << endl << endl;
		cout << sizeof(AddressRecord)<<endl<<endl;
		cout << "A - Add an entry to the address book " << endl;
		cout << "C - Change an entry in the address book" << endl;
		cout << "L - List all entries in the address book" << endl;
		cout << "S - Search the address book for a name "<<endl;
		cout << "Q - Quit the program" << endl << endl;
		cout << "Choice? ";
		cin >> choice; 
	
		cout << endl;

		if (choice == 'A' || choice == 'a')
			AddName(datafile, num_names);
		else if (choice == 'L' || choice == 'l')
			ListNames(datafile, num_names);
		else if (choice == 'C' || choice == 'c')
			change_phone(datafile, num_names);
		else if (choice == 'S' || choice == 's' )
			SearchForName();
		else if (choice == 'Q' || choice =='q')

			/* do nothing */;
		else cout << "\nInvalid option - try again.\n";

	} while (choice != 'Q' && choice != 'q');

	datafile.close();
}



In the change_phone function I need the program to skip over an entry when enter is pressed if the user decides to not change part of the record I will give you a sample dialogue of the problem.

The change option should ask for a record number and then allow each value to be changed or left the same. In the following sample dialogue, only the street and phone are changed:

Record to change? 7

Last name; Doe
Last name?
First name: John
First name?
Street: 100 South Main
Street? 105 North Maple
City: Searcy
City?
State:AR
State?
Zip: 72143
Zip?
Phone: 279-4000
Phone? 268-1234

This post has been edited by Steffan: 05 May 2008 - 01:30 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Address Book Program

#2 Zerobu   User is offline

  • Black Hatter

Reputation: 13
  • View blog
  • Posts: 1,822
  • Joined: 14-January 08

Re: Address Book Program

Posted 05 May 2008 - 03:55 PM

i really need this done, this is due tuesday at 8am
Was This Post Helpful? 0
  • +
  • -

#3 Zerobu   User is offline

  • Black Hatter

Reputation: 13
  • View blog
  • Posts: 1,822
  • Joined: 14-January 08

Re: Address Book Program

Posted 05 May 2008 - 05:38 PM

[size=7]

[size=7]
FUCK!!!!!!!!! I NEED THIS DONE!!
Was This Post Helpful? 0
  • +
  • -

#4 realNoName   User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 343
  • Joined: 04-December 06

Re: Address Book Program

Posted 05 May 2008 - 06:15 PM

dude you need to relax posting "FUCK!!!!!!!!! I NEED THIS DONE!!" is not going to get people to help you more.. sometimes it takes time for people to get to your post


as for your problem something like this should work
	//Temp var
	char buffer[30];
	cout << "Last name? ";
	//input into the temp to see if anything is going to change
	cin.getline(buffer, LAST_NAME);
	//if the first char is not null then they entered somthing 
	//so we need to copy the buffer over to the var that holds the info
	if(!buffer[0]==NULL)
		strcpy(temp.lname,buffer);

	cout << "First name? ";
	cin.getline(buffer, FIRST_NAME);
	if(!buffer[0]==NULL)
		strcpy(temp.fname,buffer);

Was This Post Helpful? 0
  • +
  • -

#5 Zerobu   User is offline

  • Black Hatter

Reputation: 13
  • View blog
  • Posts: 1,822
  • Joined: 14-January 08

Re: Address Book Program

Posted 05 May 2008 - 06:41 PM

thanks your a real life saver
Was This Post Helpful? 0
  • +
  • -

#6 Guest_Whizzy*


Reputation:

Re: Address Book Program

Posted 05 May 2008 - 07:40 PM

void AddName(fstream &datafile, int &num_names)
{
	AddressRecord temp;
	int i;

	cin.get();

	cout << "\nLast name? ";
	cin.getline(temp.lname,FIRST_NAME);

	
	cout<<"First name? ";
	cin.getline(temp.fname, FIRST_NAME);




Shouldn't one of them be LAST_NAME ???
Was This Post Helpful? 0

#7 realNoName   User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 343
  • Joined: 04-December 06

Re: Address Book Program

Posted 05 May 2008 - 10:37 PM

View PostWhizzy, on 5 May, 2008 - 07:40 PM, said:

Shouldn't one of them be LAST_NAME ???

it should have been but LAST_NAME and FIRST_NAME are just const for the size of the array and first and last just happen to be the same size so it doesn't really make a difference

This post has been edited by realNoName: 05 May 2008 - 10:44 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1