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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
|
#include "stdafx.h"
#include<iostream.h> //include input/output library
#include<string.h> //include string library
#include<fstream.h> //include file stream which allows to create files
# define MAX 100 // Variable MAX=100
struct person //create structure
{
char name[30];
char street[40];
char town[20];
char postcode[10];
}info[MAX];
//Function Load
void load()
{
ifstream records_file("RECORDS.DAT");
int i; //declare variable int i
for(i=0; i<MAX; i++);
while (!records_file.eof())
{
records_file.read ((char *) info, sizeof(info));
cout<<'\n';
};
}
//Function find_free which finds a free record
find_free()
{
register int i; //declare variable int i
for(i=0; i<MAX; i++) //initialise i
if(!*info[i].name) return i;
return -1;
}
//Function save which saves a record
void save()
{
ofstream records_file("RECORDS.DAT",ios::out|ios::app);; //open file from disk and point to end of file to avoid overwriting
if (!records_file){ //display error msg if file not found
cout << "Cannot Open File" << endl;
};
printf("\nsaving file\n");
records_file.write ((char *) info,sizeof(info)); //write to file 'RECORDS.DAT' by filling the fields of the 'info'
records_file.close(); //close file
}
//Function 'inputs'
void inputs( char *prompt , char *s , int count)
{
char str[255];
do {
printf(prompt); //Displays msg on screen like "Enter Name"
gets(str);
if(strlen(str)>=count) printf("\nToo Long! Enter Again\n"); //When the string size exceeds its limit it gives error msg
} while(strlen(str)>=count);
strcpy(s , str); //this copy's the entered data to the appropiate string of the structure
}
//Function Enter which allows user to input record(s)
void enter()
{
int i;
for(;;) {
i = find_free(); // calls function 'find_free' and finds a free structure
if(i<0) { //if i is a negative number, give error
cout<<"Error\n";
return;
}
else if (i>MAX) { //if i is greater than 100, give message 'List Full'
cout<<"List full\n";
return;
}
//call function 'input'
inputs("Enter Name: ", info[i].name,30); if(!*info[i].name) break; //if nothing is entered stop executing the function
inputs("Enter Street: ", info[i].street, 40);
inputs("Enter Town: ", info[i].town, 20);
inputs("Enter Postal Code: ", info[i].postcode, 10);
save();
}
}
//Function find which finds a specific record
find(char *name)
{
int i;
for(i=0 ; i<MAX ; i++) //initialise i
if(!strcmp(name ,info[i].name)) break; //if the name entered by the user is not the same as any in the list, end function
if(i==MAX) return -1;
else return i;
}
//Function Del which deletes a record
void del()
{
load();
int i; //declare i
char str[255]; //declare str with size limit of 255
inputs("enter name: " , str , 30); //user is asked to enter the name which has to be deleted
i = find(str); //call function 'find'
if (i>=0) *info[i].name = '\0' ;
else printf("not found\n") ;
}
//Function display
void display(int i)
{
printf("%s\n" , info[i].name);
printf("%s\n" , info[i].street);
printf("%s\n" , info[i].town);
printf("%s\n" , info[i].postcode);
}
//Function display_records which displays all records
void display_records()
{
load(); //call function 'load' to open the RECORDS.DAT file from HDD
printf("\n");
int i;
for(i=0 ; i<MAX ; i++) {
if(*info[i].name) //if records exists
display(i); //call function display which shows the full list of records
printf("\n\n");
}
printf("\n\n");
}
//Function Search
void search()
{
load(); //call function 'load' to open the RECORDS.DAT file from HDD
int i;
char name[30];
printf("\n");
inputs("Enter Name to Find: " , name , 30); //call function inputs which allows user input. User is aked to enter the key field(name) to find record
printf("\n");
if ((i=find(name))<0) //if the name entered does not exist in the record, display msg 'Not Found'
printf("Not found\n");
else display(i); //if the name entered is found, display the record.
}
//Main Function
void main(void)
{
register int i; //initialise i
for (i=0;i<MAX;i++)
*info[i].name='\0';
//ofstream records_file("RECORDS.DAT"); //create file RECORDS.DAT
//menu
int choice;
cout<<"Welcome. What would you like to do?"<<'\n';
cout<<"Create a new record - (Press '1')"<<'\n';
cout<<"Delete a record - (Press '2')"<<'\n';
cout<<"Display all records - (Press '3')"<<'\n';
cout<<"Find a record - (Press '4')"<<'\n';
cout<<"Quit - (Press '0')"<<'\n';
cin>>choice; //user inputs his/her choice
switch(choice)
{
case 1:
enter(); //calling function 'enter' which allows user to input a record
main();
break;
case 2:
del(); //delete a record
main();
break;
case 3:
display_records();
main();
break;
case 4:
search();
main();
break;
}
}
|