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
|
#include <iostream>
#include<fstream>
#include<stdlib.h>
#include<conio.h>
#include<vector>
using namespace std;
struct person
{
string name;
int age;
string dob;
};
class family
{
person p1;
vector<person> p2;
int l=0; int b=0; // b= number of entries. which is assigned to this variable in countline()
string str;
public:
void write() //function to write the record in a txt file
{
char ch='y';
while(ch=='y' || ch=='Y') // alternate: for(char ch{'y'};ch=='y' || ch=="Y");
{
system("cls");
cout<<"\nenter name: "; cin.ignore();getline(cin,p1.name);
cout<<"\nenter age: "; cin>>p1.age;
cout<<"\nenter date of birth dd/mm/yy: "; cin.ignore();getline(cin,p1.dob);
ofstream outfile;
outfile.open("record.txt",ios::out|ios::app);
outfile<<p1.name<<endl;
outfile<<p1.age<<endl;
outfile<<p1.dob<<endl;
outfile.close();
cout<<"\nRecord updated. Do you want to enter another record? ";
cin>>ch;
}
}
void read() //function reads from txt file and assign values to struct objects
{
ifstream infile("record.txt", ios::in);
if (infile) {
p2.clear();
for (person p;getline(infile,p.name) && infile >> p.age >> p.dob >> ws; p2.emplace_back(p));
infile.close();
} else
cout << "Cannot open file\n";
}
void showall() // function displays the entire record
{
system("cls");
for(person p3 : p2)
{
cout<<"\n\nname: "<<p3.name<<"\t\tage: "<<p3.age<<"\t\tdate of birth: "<<p3.dob;
}
cout<<"\n\npress any key to continue";
getch();
}
void countline() //function to count number of lines in txt file
{
ifstream infile("record.txt");
while(getline(infile,str))
{
l++;
}
infile.close();
b=l/3;
}
void modify(string n) // function to change the record of a member
{
system("cls");
ifstream file("record.txt",ios::in);
ofstream outfile("temp.txt",ios::out|ios::app);
if(file)
{
for(person p4;getline(file,p4.name)&&file>>p4.age>>p4.dob>>ws;)
{
if(p4.name==n)
{
cout<<"\nname: "<<p4.name<<"\tage: "<<p4.age<<"\tdate of birth: "<<p4.dob<<endl;
cout<<"\Modify: ";
cout<<"\nenter name: ";getline(cin,p4.name);
cout<<"\nenter age: "; cin>>p4.age;
cout<<"\nenter date of birth dd/mm/yy: "; cin.ignore();getline(cin,p4.dob);
outfile<<p4.name<<endl;
outfile<<p4.age<<endl;
outfile<<p4.dob<<endl;
cout<<"\nrecord updated!!";
cout<<"\nPress any key to continue";
_getch();
}
else
{
outfile<<p4.name<<endl;
outfile<<p4.age<<endl;
outfile<<p4.dob<<endl;
}
}
file.close();
outfile.close();
remove("record.txt");
rename("temp.txt","record.txt");
}
else
cout<<"file does not exist!!!";
}
void show(string n1) // function to show record of a particular member
{
system("cls");
ifstream infile("record.txt",ios::in);
infile.seekg(0,ios::beg);
if(infile)
{
for(const auto& p:p2)
{
if(p.name==n1)
{
cout<<"\nname: "<<p.name<<"\nage: "<<p.age<<"\ndate of birth: "<<p.dob<<endl;
cout<<"\n\npress any key to continue";
_getch();
}
else
continue;
}
infile.close();
}
else {
cout<<"file doesnot exist!!! press any key to continue.....";
_getch();
}
}
};
int main()
{
family fm;
string mod;
while(1)
{
int choice;
cout<<"---------Family record-----------"<<endl;
cout<<"\n\n1. Enter a new record"<<endl<<"2. Show all records"<<endl<<"3. Modify a record"<<endl<<"4. Show record of a member"<<endl<<"5. exit"<<endl;
cin>>choice;
switch(choice)
{
case 1:
fm.write();
break;
case 2:
fm.read();
fm.showall();
break;
case 3:
cout<<"Enter the name of family member: "; cin.ignore(); getline(cin,mod);
fm.modify(mod);
break;
case 4:
cout<<"Enter the name of family member: "; cin.ignore(); getline(cin,mod);
fm.read();
fm.show(mod);
break;
case 5:
exit(1);
break;
}
system("cls");
}
return 0;
}
|