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
|
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
const int MAX = 30;
struct Acct
{
char name[MAX];
char surName[MAX];
char title[MAX];
char job [MAX];
int floorLvl;
int rmNo;
int ext;
char userName[MAX];
};
void appendNewUser (fstream&, char *);
void updateUserFile (fstream&, char *);
int main ()
{
int option = 0;
fstream afile;
ofstream outfile;
char nameTxt[MAX];
char nameDat[MAX];
while (option !=9)
{
cout << "Main menu - Choose one of the options:" << endl;
cout << "1. Update User file" << endl;
cout << "2. Query User file" << endl;
cout << "3. Generate text file" << endl;
cout << "9. Quit\n" << endl;
cout << "Your choice: ";
cin >> option;
switch (option)
{
case 1: updateUserFile (afile, nameDat);
break;
case 2:
break;
}
}
return 0;
}
void updateUserFile (fstream& afile, char nameDat [])
{
int option = 0;
cout << "Enter user file name: ";
cin >> nameDat;
cout << "\n\nSubmenu - Two types of updates\n" << endl;
cout << "1. Append new user" << endl;
cout << "2. Update user information" << endl;
cout << "9. Quit\n" << endl;
cout << "Your choice: ";
cin >> option;
switch (option)
{
case 1: appendNewUser (afile, nameDat);
break;
case 2: exit (-1);
break;
case 9: exit (-1);
}
}
void appendNewUser (fstream& afile, char nameDat [])
{
afile.open (nameDat, ios::out | ios::in | ios::binary);
Acct user [MAX];
int i;
int size;
afile.read (reinterpret_cast <char *> (&user), sizeof (user));
afile.read (reinterpret_cast <char *> (&size), sizeof (size));
cout << "Append new user" << endl;
cout << "Given name: ";
cin.ignore ();
cin.getline (user[size].name, MAX);
cout << "Surname: ";
cin.getline (user[size].surName, MAX);
cout << "Your title: ";
cin >> user[size].title;
cout << "Your job: ";
cin.ignore();
cin.getline (user[size].job, MAX);
cout << "Office floor level: ";
cin >> user[size].floorLvl;
cout << "Office room number: ";
cin >> user[size].rmNo;
cout << "Telephone extention: ";
cin >> user[size].ext;
cout << "Email (case sensitive): ";
cin >> user[size].userName;
for ( int n = 0; n < size; n++)
{
while ( user [size].userName == user [n].userName)
{
cout << "Email exists, please re-enter another one" << endl;
cout << "Email (case sensitive): ";
cin >> user[size].userName;
}
}
cout << user [size].userName << " was appended to file" << endl;
size++;
cout << size << endl;
afile.write (reinterpret_cast <const char *> (&user), sizeof (user));
afile.write (reinterpret_cast <const char *> (&size), sizeof (size));
afile.close ();
}
|