How to store entries once entered?

Im a beginner at C++ . And this is the project im asked to do. i wanna know how to store phone records and retrieve it even after the program is closed.

#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
#include<process.h>
void main()
{clrscr();
struct direct
{ char na[20],dist[20],state[20],sex;
long double pno;}s[50],b;
int y,m,e,z;
for(int g=0;g!=1;)
{ clrscr();
cout<<" *----------------------------------------* \n";
cout<<" | 1| TELEPHONE DIRECTORY | \n";
cout<<" |==|=====================================| \n";
cout<<" | 2| INSTRUCTIONS | \n";
cout<<" |==|=====================================| \n";
cout<<" | 3| CREDITS | \n";
cout<<" |==|=====================================| \n";
cout<<" | 4| EXIT | \n";
cout<<" *----------------------------------------* \n\n\n";
cout<<"\n\n\t ENTER YOUR CHOICE ";
cin>>y;
if(y<0&&y>5)
continue;
clrscr();
switch(y)
{{
case 1:
clrscr();
cout<<"ENTER NUMBER OF PEOPLE: ";
int n;
cin>>n;
for(int i=0;i<n;i++)
{clrscr();
cout<<"\n"<<i+1<<".\n"<<"NAME: ";
gets(s[i].na);
cout<<"PHONE NUMBER: ";
cin>>s[i].pno;
cout<<"STATE: ";
gets(s[i].state);
cout<<"DISTRICT: ";
gets(s[i].dist);
cout<<"SEX(M/F): ";
cin>>s[i].sex;
for(int k=0;k!=1;)
{if(s[i].sex!='m'&&s[i].sex!='f'&&s[i].sex!='F'&&s[i].sex!='M')
{clrscr();
cout<<"\n"<<i+1<<".\n"<<"NAME: ";
puts(s[i].na);
cout<<"PHONE NUMBER: ";
cout<<s[i].pno<<endl;
cout<<"STATE: ";
puts(s[i].state);
cout<<"DISTRICT: ";
puts(s[i].dist);
cout<<"SEX(M/F): ";
cin>>s[i].sex;}
else
k=1;}
}
for(i=0;i<n-1;i++)
for(int j=0;j<n-1;j++)
if(strcmp(s[j].na,s[j+1].na)>0)
{b=s[j];
s[j]=s[j+1];
s[j+1]=b;
}clrscr();
do
{cout<<"+--------------------+ \n";
cout<<"| NAMES IN | \n";
cout<<"| ORDER | \n";
cout<<"|====================| \n";
for(i=0;i<n;i++)
{cout<<"|"<<i+1<<" : "<<s[i].na;
for(j=strlen(s[i].na)-1;j<=14;j++)
cout<<" ";
cout<<"|\n";
cout<<"| |\n";
cout<<"|====================|\n";
cout<<"| |\n";}
cout<<"| |\n";
cout<<"+--------------------+\n";
cout<<"\n\n"<<"ENTER NO:";
cin>>e; }
while(e>n);
clrscr();
cout<<"+---------------------+ \n";
cout<<"| | \n";
cout<<"|NAME:-"<<s[e-1].na;
for(j=strlen(s[e-1].na);j<=15;j++)
cout<<" ";
cout<<"\n";
cout<<"| | \n";
cout<<"|=====================| \n";
cout<<"| | \n";
cout<<"|STATE:-"<<s[e-1].state;
for(j=strlen(s[e-1].state);j<=15;j++)
cout<<" ";
cout<<"\n";
cout<<"| | \n";
cout<<"|=====================| \n";
cout<<"| | \n";
cout<<"|DISTRICT:-"<<s[e-1].dist;
for(j=strlen(s[e-1].dist);j<=15;j++)
cout<<" ";
cout<<"\n";
cout<<"| | \n";
cout<<"|=====================| \n";
cout<<"| | \n";
cout<<"|PHONE:-"<<s[e-1].pno;
for(j=strlen(s[e-1].pno);j<=15;j++)
cout<<" ";
cout<<"\n";
cout<<"| | \n";
cout<<"|=====================| \n";
cout<<"| | \n";
cout<<"|SEX:-"<<s[e-1].sex;
for(j=7;j<=18;j++)
cout<<" ";
cout<<"\n";
cout<<"| | \n";
cout<<"+---------------------+ \n";
cout<<"\n\n";

do
{
getch();
clrscr();
cout<<" *----------------------------------* \n";
cout<<" |1 | GO TO MAIN MENU | \n";
cout<<" |==|===============================| \n";
cout<<" |2 | EXIT | \n";
cout<<" *----------------------------------* \n";
cout<<"ENTER YOUR OPTION ";
cin>>m;
}
while(m!=1&&m!=2);
if(m==1)
continue;
else if(m==2)
exit(0);
break;}

case 2:do
{clrscr();
cout<<" \n";
cout<<" THIS PROGRAM IS CALLED TELEPHONE DIRECTORY \n ";
cout<<" PLEASE DO AS DIRECTED:- \n ";
cout<<" USE NUMPAD TO SELECT THE OPTIONS\n \n";
cout<<"\n\n\n\n";
cout<<" *----------------------------------* \n";
cout<<" |1 | TO GO TO MAIN MENU | \n";
cout<<" |==|===============================| \n";
cin>>m;
if(m==1)
break;
if(m==2)
cout<<"PLEASE DO AS DIRECTED\n";
}while(m!=1&&m!=2);break;
case 3:
do
{clrscr();
cout<<" \aTHE\a PROJECT\a IS SUBMITTED\a BY\n\tROHIT\a\n &&GOPIKRISHNAN\n\a\n\n WE CONVEY OUR SINCERE THANKS TO SUJA TEACHER\n\a FOR GIVING US THE\a\n\n \nOPPORTUNITY\n\a\tWE ALSO LIKE TO THANK ALL OTHER PEOPLE\n\n\a WHO SUPPORTED US\n\n\n\n\n\t\a..\aTHANK\n\t\a YOU\a...\a " ;
cout<<"\n\n";
cout<<" *----------------------------------* \n";
cout<<" |1 | TO GO TO MAIN MENU | \n";
cout<<" |==|===============================| \n";
cout<<" |2 | EXIT | \n";
cout<<" *----------------------------------* \n";
cin>>m;}
while(m!=1 && m!=2);
if(m==1)
continue;
if(m==2)
exit(0);
break;
case 4:exit(0);break;
default:
clrscr();
continue;
}}}

Once a program has ended it's memory is wiped out so you will need to write data to a file or database to preserve them.

Once you data is stored in variables just write them to file like so. More detail is written on this site in the reference section. => http://www.cplusplus.com/reference/iostream/fstream/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<fstream>  //You don't need .h for C++ libraries, .h is for C ones

ifstream inFile;  // used to read from files
ofstream outFile;  / used to write to files

string data;

outFile.open("DataFile");  // open file to write data to
outFile << data;  // write actually data
outFile.close();

inFile.open("DataFile");  // open prev written data file
if (inFile)  // if file opened correctly 
    inFile >> data;  // read data back into the string variable 







Last edited on
Topic archived. No new replies allowed.