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]

Dani AI

Generated

A compact, practical checklist and fixes that fill gaps in the thread (ties to and ):

As suggested, avoid calling main() recursively — run the menu inside a loop (while/for) and break on Quit. The "skipped Name" symptom that fixed is the classic mix of operator>> and getline: operator>> leaves the newline in the input buffer, so the next getline() immediately returns an empty string. Prefer a consistent input strategy (getline for text), or consume the leftover newline.

Use modern, safe types and a single-file layout:

  • store textual fields (name, address, phone) as std::string (phone numbers as strings preserve formatting and avoid integer overflow),
  • keep contacts in a dynamic container like std::vector<Contact>,
  • persist contacts in one file (append new lines or rewrite the whole file on save) rather than one file per contact.

Small, practical snippets (apply immediately):

#include <limits>
// consume leftover newline after using operator>>
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

// simple text-record format: field|field|field\n
std::ofstream out("book.txt", std::ios::app);
out << c.name << '|' << c.email << '|' << c.phone << '\n';

Load/save + search pattern: read the file at startup into a vector (parse each line with std::getline and std::istringstream using '|' as delimiter), perform linear search (or sort + binary search for large sets), and on modify/delete rewrite the file with ios::trunc. Always check file.open() success (fail()).

Troubleshooting checklist:

  • If fields are skipped, check mixing of >> and getline and use cin.ignore() or use getline everywhere.
  • If file content is overwritten, use ios::app to add or open with ios::trunc only when intentionally rewriting.
  • If phone input crashes or truncates, switch phone fields to std::string.
  • Avoid writing raw struct memory to disk unless fixed-layout binary format and readers agree.

Core topics to study: fstream file I/O, std::string and std::getline, std::vector and containers, simple text serialization/parsing, and input validation/error handling.

Recommended Answers

All 5 Replies

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...

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.

Read this, please.

ok for your first issue the Inabillity to Name your new contact after creating the file i added another "cin.getline(Name,40);"
to the code just before the action is requested by the programme now the stage being skiped is no one of no importance

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);
       cin.getline(Name,40);
	cout<<"Name: "; 
	cin.getline(Name,40);
	cout<<""<<endl;

Also the other problem you had of telephone numbers crashing/reseting the programme if more that a few numbers are used ... I have no idea why that is happening but if you can afford to .. redefine the Telephone numbers as Char's
like this

char Home[12];
char Business[12];
char Cell[12];
char Other[12];

or as strings

stringHome;
stringBusiness;
stringCell;
stringOther;

or you could use plain simple strings it all depends and what you are goin to be doing with the data after if has been created

If they are still working on this more than 3.5 years later, there is something very wrong. ;)

I suppose it could still be of use to someone that comes across this searching for help with their own problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.