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
|
#include <iostream>
#include <cstring>
#include <fstream>
#include <cstdlib>
using namespace std;
bool LoadFile(char[], int&); // takes the filename to open - returns bool
void AddSubscriber(int&, char[] );
void DeleteSubscriber();
void ChangeExpDate();
void ChangeAddress();
void PrintSubscriber();
void PrintExpired();
void SaveFile(char[], int&); // takes a filename as an argument
void PrintMenu(); // print menu function
const int MAX = 100;
const int FMAX = 32;
struct subscriber
{
char firstname [MAX];
char lastname [MAX];
char street [MAX];
char city [MAX];
char state [MAX];
int postcode;
int sub_expmonth;
int sub_expyear;
};
int main()
{
char command;
char Filename[FMAX];
subscriber collection[MAX]; // collection of subscriber records
int tally; // you might need to introduce another variable - can you think why?
cout << "Enter a filename to load database: ";
cin >> Filename;
LoadFile(Filename, tally);
PrintMenu();
do
{
cout << "\nCommand:" << endl;
cin >> command;
switch (command)
{
case 'A':
AddSubscriber(tally ,collection); // YOU WILL NEED TO CHANGE THE CALL
break;
/*case 'D':
DeleteSubscriber( &collection, &tally ); // YOU WILL NEED TO CHANGE THE CALL
break;
case 'C':
ChangeExpDate( &collection, &tally ); // YOU WILL NEED TO CHANGE THE CALL
break;
case 'R':
ChangeAddress( &collection, &tally ); // YOU WILL NEED TO CHANGE THE CALL
break;
case 'P':
PrintSubscriber( &collection, &tally ); // YOU WILL NEED TO CHANGE THE CALL
break;
case 'E':
PrintExpired( &collection, &tally ); // YOU WILL NEED TO CHANGE THE CALL
break;*/
case 'S':
SaveFile( Filename, tally); // YOU WILL NEED TO CHANGE THE CALL
break;
case 'Q':
break;
default:
cerr << "Illegal Command\n";
}
if (command != 'Q') // don't output menu if quiting
PrintMenu();
} while (command != 'Q');
return 0;
}
void PrintMenu()
{
cout << "Commands Available:\n\n";
cout << " A - add a new subscriber\n";
cout << " C - change expiry date for a specified subscriber\n";
cout << " D - delete a subscriber\n";
cout << " R - change address for a specified subscriber\n";
cout << " P - print a specified subscriber\n";
cout << " E - print subscribers that have expired\n";
cout << " S - save the in memory records back to the file\n";
cout << " Q - terminate the program" << endl;
}
bool LoadFile (char Filename [FMAX], int &tally)
{
subscriber collection [MAX];
Filename = { Filename };
ifstream saucy;
saucy.open ( Filename, ios::in );
if ( saucy.fail() )
{
cout << "The file " << Filename << " has failed to open." << endl;
cout << "A new, blank database has been automatically created using the inputted name.\n" << endl;
ofstream saucy;
saucy.open ( Filename, ios::out ); //Creates file under inputted name
saucy << "0" << '\n'; //Writes record count (0 at the moment) to first line, ready for use
saucy.close();
return false;
}
else if ( saucy.good() )
{
cout << "The file " << Filename << " loaded successfully." << endl;
saucy >> tally, '\n'; //Reads first line of file which provides the record count
cout << tally << " records have been read into memory.\n" << endl;
for( int i = 0; i < tally; i ++ )
{
saucy >> collection[i].firstname;
saucy >> collection[i].lastname;
saucy >> collection[i].street;
saucy >> collection[i].city;
saucy >> collection[i].state;
saucy >> collection[i].postcode;
saucy >> collection[i].sub_expmonth;
saucy >> collection[i].sub_expyear;
}
for(int i = 0; i < tally; i ++ )
{
cout << collection[i].firstname << endl;
cout << collection[i].lastname << endl;
cout << collection[i].street << endl;
cout << collection[i].city << endl;
cout << collection[i].state << endl;
cout << collection[i].postcode << endl;
cout << collection[i].sub_expmonth << endl;
cout << collection[i].sub_expyear << endl;
}
return true;
}
saucy.close();
}
void AddSubscriber(int &tally, subscriber &collection [MAX])
{
int tempcount,i;
tempcount = tally + 1;
cout << "Please enter the new subscribers first name - ";
cin >> collection[tempcount].firstname;
cout << "Please enter the new subscribers last name - ";
cin >> collection[tempcount].lastname;
cout << "Please enter the new subscribers street - ";
cin >> collection[tempcount].street;
cout << "Please enter the new subscribers city - ";
cin >> collection[tempcount].city;
cout << "Please enter the new subscribers state - ";
cin >> collection[tempcount].state;
cout << "Please enter the new subscribers postcode - ";
cin >> collection[tempcount].postcode;
cout << "Please enter the new subscribers subscription expiration month - ";
cin >> collection[tempcount].sub_expmonth;
cout << "Please enter the new subscribers subscription expiration year - ";
cin >> collection[tempcount].sub_expyear;
cout << "The new details have been taken into memory." << endl;
cout << "Please use the 'S' option from the menu to save this subscriber to the database." << endl;
tally++;
}
void SaveFile(char Filename [FMAX], int &tally)
{
int i = 1, j;
subscriber collection[MAX];
Filename = { Filename };
cout << "Major checkpoint - " << tally << endl;
ofstream saucy;
saucy.open ( Filename, ios::out );
saucy << tally, '\n';
for(i = 0; i < tally; i ++ )
{
saucy << collection[i].firstname;
saucy << collection[i].lastname;
saucy << collection[i].street;
saucy << collection[i].city;
saucy << collection[i].state;
saucy << collection[i].postcode;
saucy << collection[i].sub_expmonth;
saucy << collection[i].sub_expyear;
}
saucy.close();
}
|