RSS Forums RSS
Please support our C++ advertiser: Programming Forums
Views: 1952 | Replies: 3 | Solved
Reply
Join Date: Jul 2005
Posts: 6
Reputation: pscha3 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
pscha3 pscha3 is offline Offline
Newbie Poster

Need help with Address book

  #1  
Jul 24th, 2005
Hello,
I just started C++ and I have this assignment to write. I started writing it but I can't seem to get this one thing and that is how to create a new contact and store it in a file.
I need to know if I used the structure correctly. Also I don't know how you create multiple contacts with a loop. I wouldn't necessarily need the answer from you guys. I just want to know what topics I need to learn to be able to do this.
What do I need to use? That's all I need. I can write the programs for myself.

Also if you guys can tell me how you search for and display a single contact. That would be helpful. Thanks for understanding.

pscha3

Here's the code:
//This is the term project by Chaitanya Pillalamarri. Class CS-102.
#include<iostream>
#include<iomanip>
#include<cmath>
#include<cstdlib>
#include<fstream>
using namespace std;

void Display();
void Delete();
void Create();
void Modify();
void Search();
void Quit(void);
void Restart(int);

int Choice=0;

int main()
{
	//The following output statements are the menu.
	cout<<""<<endl;
	cout<<"                         Welcome to Virtual Address Book                    "<<endl;
	cout<<"In this book, you have a variety of features to revolutionize your contact lists"<<endl;
	cout<<""<<endl;
	cout<<"Follow the directions below and chose appropriately from the menu."<<endl;
	cout<<""<<endl;
	
	//These are the choices.
	cout<<"Please type in the following number to perform the following task:"<<endl;
	cout<<" 1 - Display all your contacts\n";
	cout<<" 2 - Create a new contact\n";
	cout<<" 3 - Edit information for a contact\n";
	cout<<" 4 - Search for a contact\n";
	cout<<" 5 - Delete contact\n";
	cout<<" 6 - Quit this application\n";
	cout<<""<<endl;
	cout<<""<<endl;
	cout<<"If the application asks you to restart the program, just type in '7'."<<endl;
	cout<<" 7 - Restarts program"<<endl;
	cout<<""<<endl;
	
	//The user choice.
	cout<<"ENTER YOUR CHOICE:"<<endl;
	cin>>Choice;

	switch(Choice)
	{		
		case 1:
		{
			Display();
			break;
		}
		case 2:
		{
			Create();
			break;
		}
		case 3:
		{
			Modify();
			break;
		}
		case 4:
		{
			Search();
			break;
		}
		case 5:
		{
			Delete();
			break;
		}
		case 6:
		{
			Quit();
			break;
		}
		case 7:
		{
			Restart(Choice);
			break;
		}
	}
}

void Restart(int Choice)
{
	while(Choice==7)
		main();
}
void Quit(void)
{
	cout<<"Goodbye!"<<endl;
	exit(0);
}
void Create()
{
	char Name1;
	Name1=0;
	struct Contact
	{
		char Name;
		int Home;
		int Business;
		int Cell;
		int Other;
		char Email;
		char Address;
		int Zip;
		char City;
		char State;
		char Country;
		char Job;
		char Company;
	};
	Contact hi;
	ofstream Data;
	cout<<"This will let you create a new contact."<<endl;
	cout<<"Please provide the following information."<<endl;
	cout<<"When you do not want to give information for a certain field, just press enter to skip it."<<endl;
	
	//This is the place where the user enters the information.
	
	Data.open("Addressbook.txt",ios::out);
	cout<<"Name: "; 
	cin>>hi.Name;
	cout<<""<<endl;
	cout<<"Job title: "; 
	cin>>hi.Job;
	cout<<"Company: "; 
	cin>>hi.Company;
	cout<<""<<endl;
	cout<<"Address: "; 
	cin>>hi.Address;
	cout<<"City: "; 
	cin>>hi.City;
	cout<<"State: "; 
	cin>>hi.State;
	cout<<"Zip: "; 
	cin>>hi.Zip;
	cout<<"Country: "; 
	cin>>hi.Country;
	cout<<"E-mail: "; 
	cin>>hi.Email;
	cout<<"Home: "; 
	cin>>hi.Home;
	cout<<"Business: "; 
	cin>>hi.Business;
	cout<<"Cell: "; 
	cin>>hi.Cell;
	cout<<"Other: "; 
	cin>>hi.Other;
	Data.close();
}
void Display()
[edit]Code tags added. Learn how to use them before posting again. -Narue[/edit]
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2005
Posts: 33
Reputation: shre86 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 1
shre86 shre86 is offline Offline
Light Poster

Re: Need help with Address book

  #2  
Jul 25th, 2005
The use of ur structure seems fine...
To input multiple contacts just put an infinite while loop to enclose ur switch statment... so u will quit whenever u click quit.. that way u can get rid of the restart command..
For searching...
U should be storing ur contacts in an array of structures..now u r overwriting whenever the user says he wants to add a contact.. so just change that also.. so whenever u r supposed to search.. just ask for the field and find that entry that u require by a simple linear search...
Reply With Quote  
Join Date: Jul 2005
Posts: 6
Reputation: pscha3 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
pscha3 pscha3 is offline Offline
Newbie Poster

Re: Need help with Address book

  #3  
Jul 25th, 2005
Yea actually after an hour or so after my first post I totally knew how to do it. Read my book again. So yea. My only question is how do I get more memory for storing data. Since in my new program, here it is
//This is the term project by Chaitanya Pillalamarri. Class CS-102.
#include<iostream>
#include<iomanip>
#include<cmath>
#include<cstdlib>
#include<fstream>
#include<stdlib.h>
#include<stdio.h>
using namespace std;

void Display();
void Delete();
void Create();
void Modify();
void Search();
void Quit(void);
void Restart(int);

int Choice=0;
char ch;
fstream Data;
struct Contact
	{

		char Email[46];
		char Address[46];
		int Zip;
		char City[26];
		char State[26];
		char Job[46];
		char Company[46];
	};
char Name[41];
char Country[56];
int Home;
int Business;
int Cell;
int Other;
char Filename[101];
Contact hi;
int main()
{
	//The following output statements are the menu.
	cout<<""<<endl;
	cout<<"                         Welcome to Virtual Address Book                    "<<endl;
	cout<<"In this book, you have a variety of features to revolutionize your contact lists"<<endl;
	cout<<""<<endl;
	cout<<"Follow the directions below and chose appropriately from the menu."<<endl;
	cout<<""<<endl;
	
	//These are the choices.
	cout<<"Please type in the following number to perform the following task:"<<endl;
	cout<<" 1 - Display all your contacts\n";
	cout<<" 2 - Create a new contact\n";
	cout<<" 3 - Edit information for a contact\n";
	cout<<" 4 - Search for a contact\n";
	cout<<" 5 - Delete contact\n";
	cout<<" 6 - Quit\n";
	cout<<""<<endl;
	cout<<""<<endl;
	cout<<"If you want to return to this screen, just type in '7'."<<endl;
	cout<<" 7 - Returns to main menu"<<endl;
	cout<<""<<endl;
	
	//The user choice.
	cout<<"Enter your choice:"<<endl;
	cin>>Choice;

	switch(Choice)
	{		
		case 1:
		{
			Display();
			break;
		}
		case 2:
		{
			Create();
			break;
		}
		case 3:
		{
			Modify();
			break;
		}
		case 4:
		{
			Search();
			break;
		}
		case 5:
		{
			Delete();
			break;
		}
		case 6:
		{
			Quit();
			break;
		}
		case 7:
		{
			Restart(Choice);
			break;
		}
		default:
		{
			cout<<"**********************************************"<<endl;
			cout<<"* Error: You have entered a wrong choice     *"<<endl;
			cout<<"* Please follow menu instructions.           *"<<endl;
			cout<<"* The program will now restart.              *"<<endl;
			cout<<"**********************************************"<<endl;
			main();
		}
	}
}

void Restart(int Choice)
{
	while(Choice==7)
		main();
}
void Quit(void)
{
	cout<<"Goodbye!"<<endl;
	exit(0);
}
void Create()
{
	cout<<""<<endl;
	cout<<"*This will let you create a new contact*"<<endl;
	cout<<"*Please provide the following information*"<<endl;
	cout<<"*When you want to skip a filed, just press enter*"<<endl;
	cout<<""<<endl;
	cout<<"Please enter a file name for the contact."<<endl;
	cout<<"We recommend using the person's name."<<endl;
	cin>>Filename;
	cout<<""<<endl;
	cout<<"Creating..."<<endl;
	cout<<""<<endl;
	
	//This is the place where the user enters the information.
	Data.open(Filename,ios::out);
	cout<<"Name: "; 
	cin.getline(Name,40);
	cout<<""<<endl;
	cout<<"Job title: "; 
	cin.getline(hi.Job,45);
	cout<<"Company: "; 
	cin.getline(hi.Company,45);
	cout<<""<<endl;
	cout<<"Address: "; 
	cin.getline(hi.Address,45);
	cout<<"City: "; 
	cin.getline(hi.City,25);
	cout<<"State: "; 
	cin.getline(hi.State,25);
	cout<<"Zip: "; 
	cin>>hi.Zip;
	cout<<"Country: "; 
	cin.getline(Country,50);
	cout<<"E-mail: "; 
	cin.getline(hi.Email,45);
	cout<<"Home: "; 
	cin>>Home;
	cout<<"Business: "; 
	cin>>Business;
	cout<<"Cell: "; 
	cin>>Cell;
	cout<<"Other: "; 
	cin>>Other;

	Data<<""<<endl;
	Data<<"Name: "<<Name<<endl;
	Data<<""<<endl;
	Data<<"Job title: "<<hi.Job<<endl;
	Data<<"Company: "<<hi.Company<<endl;
	Data<<""<<endl;
	Data<<"Address: "<<hi.Address<<endl;
	Data<<"City: "<<hi.City<<endl;
	Data<<"State: "<<hi.State<<endl;
	Data<<"Zip: "<<hi.Zip<<endl;
	Data<<"Country: "<<Country<<endl;
	Data<<""<<endl;
	Data<<"E-mail: "<<hi.Email<<endl;
	Data<<""<<endl;
	Data<<"Home: "<<Home<<endl;
	Data<<"Business: "<<Business<<endl;
	Data<<"Cell: "<<Cell<<endl;
	Data<<"Other: "<<Other<<endl;
	Data<<""<<endl;
	Data.close();

	cout<<"Do you want to return to the main menu?(7=restart,6=Exit)"<<endl;
	cin>>Choice;
	if(Choice==7)
		main();
	else
		return;
}
void Display()
{
  char ch;
  
  Data.open(Filename,ios::in);    
  
  if(Data.fail())
  {
    cout << "Error: Unable to open file."<<endl;
    exit(1);
  }
  Data.get(ch);                    
  while(!Data.eof())
  {
    cout << ch;                 
    Data.get(ch);                
  }
  
  Data.close();                  
}
void Modify()
{

}
void Search()
{
	cout<<""<<endl;
	cout<<"To start searching the directory follow the instructions."<<endl;
	cout<<"Please enter a file name."<<endl;
	cin>>Filename;

	Data.open(Filename,ios::in);
	cout<<"Searching...."<<endl;
	cout<<""<<endl;
	if(!Data)
	{
		cout<<Filename<<" could not be opened."<<endl;
		cout<<"Please check your spelling and try again."<<endl;
		Search();
	}
	Data.get(ch);
	while(!Data.eof())
	{
		cout<<ch;
		Data.get(ch);
	}
	Data.close();

	cout<<"Do you want to search for another contact?(0 for yes)"<<endl;
	cout<<"Or go back to the main menu?(7 for main menu)"<<endl;
	cin>>Choice;
	if(Choice==7)
		main();
	else if(Choice==0||Choice==0)
		Search();
}
void Delete()
{
}

When i create a new contact, it skips the first field which is Name. And it overwrites a lot of time. Like if I want to put in a phone number, If I put like more than three numbers it skips all the other fields and goes directly to the end.
Reply With Quote  
Join Date: Sep 2004
Posts: 6,585
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 31
Solved Threads: 501
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: Need help with Address book

  #4  
Jul 25th, 2005
Read this, please.
I'm here to prove you wrong.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 1:09 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC