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 204 205 206 207 208 209 210 211 212
|
//header declaration
#include <iostream>
#include <cstdio>// same like iostream but can use old c libbries
#include <cstring>// same like string but older than string
#include <cstdlib>// inherited from c
#include <iomanip>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <string>
#include <fstream>
using namespace std;
void addrecord(); // function to add new members
void listrecord(); //function to view members as a table
void modifyrecord(); //function to update member details
void searchrecord(); //function to search member details
string username,password;
char another='y',answer;
struct member{
std::string first_name;
std::string last_name;
std::string spcl;
int age,height,weight,memID,address;
std::string ID;
void writeToFile()
{
std::ofstream outputFile;
outputFile.open("structchk.txt", std::ios::app);
outputFile << memID <<" "<< first_name << " " << last_name << " " << age << " " << address << std::endl;
outputFile.close();
}
};
string xfirst_name;
struct member cAMemberList[100];//Member array
member e; //struct member e also can be used
int recsize = sizeof(e);
int main(){
char attempt='y';
char n;
cout << "\n\n";
cout << "\n \t\t\t 1. Add New Member details";
cout << "\n \t\t\t 2. View Members' table";
cout << "\n \t\t\t 3. Search Member details";
cout << "\n \t\t\t 4. Edit existing member details";
cout << "\n\n";
cout << "\t\t\t Select Your Choice :=> ";
fflush(stdin);
cin>>n;// choice is for switch case
switch(n){
case '1':
addrecord();
break;
case '2':
listrecord();
break;
case '3':
searchrecord();
break;
case '4':
modifyrecord();
break;
default:
cout<<"ENTER VALID OPERATOR"<<endl;
}
}
void addrecord(){
another ='Y';
while(another == 'Y' || another == 'y')
{
system("cls");
cout << "Enter the ID : ";
cin >> e.memID;
cout << "Enter the Firt Name : ";
cin >> e.first_name;
cout << "Enter the Last Name : ";
cin >> e.last_name;
cout << "Enter the age : ";
cin >> e.age;
cout << "Enter the Address : ";
cin >> e.address;
e.writeToFile();
cout << "\n Add Another Record (Y/N) ";
fflush(stdin);
another = getchar();
}
}
void listrecord(){
system("cls");
string line;
ifstream myfile ("structchk.txt");
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
cout << line << '\n';
}
myfile.close();
}
else cout << "Unable to open file";
cout << "\n\n";
system("pause");
}
void searchrecord(){
ifstream fin;
fin.open("structchk.txt");
if(fin.fail())
{
cout << "Input file opening failed.\n";
exit(1);
}
string search;
cout << "Please enter a name: ";
cin >> search;
bool isFound=0;
while(!fin.eof())
{
string temp = "";
getline(fin,temp);
for(int i=0;i<search.size();i++) //search.size is extracted material
{
if(temp[i]==search[i])
isFound = 1;
else
{
isFound =0;
break;
}
}
if(isFound)
{
cout << "Details are: ";
for(int i = search.size()+1;i<temp.size();i++)
cout << temp[i];
break;
}
}
if(fin.eof()&&(!isFound))
{
cout << "Name not found!\n";
}
fin.close();
}
void modifyrecord(){
member e;
string xlast_name;
recsize=sizeof(e);
FILE *fp;
//outputFile.open("structchk.txt", std::ios::app);
fp = fopen("structchk.txt", "wb+");
system("cls");
another = 'Y';
while (another == 'Y'|| another == 'y')
{
cout << "\n Enter the last name of the student : ";
cin >> xlast_name;
const char* a=e.last_name.c_str();
const char* b=xlast_name.c_str();
rewind(fp);
while (fread(&e,recsize,1,fp) == 1)
{
if (strcmp(a,b) == 1)//was0
{
cout << "Enter new the Firt Name : ";
cin >> e.first_name;
cout << "Enter new the Last Name : ";
cin >> e.last_name;
cout << "Enter new the Age : ";
cin >> e.age;
cout << "Enter new the Address : ";
cin >> e.address;
fseek(fp, - recsize, SEEK_CUR);
fwrite(&e,recsize,1,fp);
break;
}
else
cout<<"record not found";
}
cout << "\n Modify Another Record (Y/N) ";
fflush(stdin);
another = getchar();
}
}
|