1. the loading of data from file should be achieved by overloading the stream extraction operator (>>)
2. the saving of data to file should be achieved by overloading the stream insertion operator (<<).
I have looked up it is to do with overloading the operators but I cannot figure out how to do this. Could anyone please help?
Expand|Select|Wrap|Line Numbers
- #include <iostream>
- #include <fstream>
- #include <string.h>
- #include <algorithm>
- #include <string>
- using namespace std;
- #define DATABASE_MAX 10
- #define DATABASE_FILENAME "database.txt"
- class OpAmps
- {
- private:
- string Name;
- unsigned int PinCount;
- double SlewRate;
- public:
- void Enter(OpAmps&, unsigned long&);
- void Save(const OpAmps*, unsigned long);
- void Load(OpAmps*, unsigned long&);
- void Sort(OpAmps*, unsigned long);
- void Display(const OpAmps*, unsigned long);
- friend bool SortName(const OpAmps &, const OpAmps &);
- friend bool SortSlewRate(const OpAmps &, const OpAmps &);
- };
- int main()
- {
- OpAmps OpAmp[DATABASE_MAX]; // the database
- OpAmps Menu;
- unsigned long database_length = 0; // the number of elements in the database
- char UserInput;
- // loop until the user wishes to exit
- while (1) {
- // show the menu of options
- cout << endl;
- cout << "Op-amp database menu" << endl;
- cout << "--------------------" << endl;
- cout << "1. Enter a new op-amp into the database" << endl;
- cout << "2. Save the database to disk" << endl;
- cout << "3. Load the database from disk" << endl;
- cout << "4. Sort the database" << endl;
- cout << "5. Display the database" << endl;
- cout << "6. Exit from the program" << endl << endl;
- // get the user's choice
- cout << "Enter your option: ";
- cin >> UserInput;
- cout << endl;
- // act on the user's input
- switch(UserInput)
- {
- case '1':
- Menu.Enter(OpAmp[database_length], database_length);
- break;
- case '2':
- Menu.Save(OpAmp, database_length);
- break;
- case '3':
- Menu.Load(OpAmp, database_length);
- break;
- case '4':
- Menu.Sort(OpAmp, database_length);
- break;
- case '5':
- Menu.Display(OpAmp, database_length);
- break;
- case '6':
- return 0;
- default:
- cout << "Invalid entry" << endl << endl;
- break;
- }
- }
- }
- void OpAmps::Enter(OpAmps& Op, unsigned long& database_length)
- {
- // if the database is full, inform the user
- if (database_length >= DATABASE_MAX) {
- cout << "The database is full" << endl;
- }
- // if the database is not full, get the data from the user and alter the database length
- else {
- cout << "Add new data" << endl;
- cout << "------------" << endl;
- cout << "Enter Operational Amplifier name: ";
- cin >> Op.Name;
- cout << "Enter number of pins: ";
- cin >> Op.PinCount;
- cout << "Enter slew rate: ";
- cin >> Op.SlewRate;
- cout << endl;
- cout << endl << "Operational Amplifier data is now in memory" << endl;
- cout << endl << "Select option 1 from main menu to enter another data into the database" << endl;
- cout << "select option 2 from main menu to save the database to the disk" << endl;
- database_length++;
- }
- }
- void OpAmps::Save(const OpAmps* Op, unsigned long database_length)
- {
- fstream output_file; // file stream for output
- // open the file
- output_file.open(DATABASE_FILENAME, ios::out);
- // write length information to file
- if(!output_file.good()) {
- cout << "FATCH ERROR: Could not open file";
- exit (1);
- }
- cout << "Opened file to save data to database" << endl << endl;
- // write length information to file
- output_file << database_length << endl << endl;
- // write data to file
- for (unsigned long i=0;i<database_length;i++) {
- output_file << Op[i].Name << endl;
- output_file << Op[i].PinCount << endl;
- output_file << Op[i].SlewRate << endl << endl;
- }
- if(database_length <= 10) {
- cout << database_length << " entry(s) added to database" << endl;
- }
- else
- {
- cout << "10 entry(s) added to database" << endl;
- }
- // close the file
- output_file.close();
- }
- void OpAmps::Load(OpAmps* Op, unsigned long& database_length)
- {
- fstream input_file; // file stream for input
- // open the file
- input_file.open(DATABASE_FILENAME, ios::in);
- // load length information from file
- if(!input_file.good()) {
- cout << "FATCH ERROR: Could not open file";
- exit (1);
- }
- input_file >> database_length;
- if(database_length < 10) {
- cout << "The data length now is: " << database_length << endl << endl;
- cout << "Select option 5 from main menu to display the data loaded from disk" << endl;
- }
- else
- {
- cout << "The data length now is: 10" << endl << endl;
- cout << "Select option 5 from main menu to display the data loaded from disk" << endl;
- }
- // close the file
- input_file.close();
- }
- void OpAmps::Sort(OpAmps* Op, unsigned long database_length)
- {
- char UserInput;
- cout << endl;
- cout << "Sorting options" << endl;
- cout << "---------------" << endl;
- cout << "1. To sort by name" << endl;
- cout << "2. To sort by slew rate" << endl;
- cout << "3. No sorting" << endl << endl;
- // get the user's choice of sorting operation required
- cout << "Enter your option: ";
- cin >> UserInput;
- cout << endl;
- // act on the user's input
- switch(UserInput) {
- case '1':
- cout<<"sortName"<<endl;
- std::sort(Op, Op + database_length, SortName);
- cout << "The database has been sorted by name, select option 5 to show the database!" << endl << endl;
- break;
- case '2':
- cout<<"sortslew"<<endl;
- std::sort(Op,Op + database_length, SortSlewRate);
- cout << "The database has been sorted by Slew Rate, select option 5 to show the database!" << endl << endl;
- break;
- case '3':
- return;
- default:
- cout << "Invalid entry" << endl << endl;
- break;
- }
- }
- bool SortName(const OpAmps &First, const OpAmps &Second)
- {
- return First.Name < Second.Name;
- }
- bool SortSlewRate (const OpAmps &First, const OpAmps &Second)
- {
- return First.SlewRate < Second.SlewRate;
- }
- void OpAmps::Display(const OpAmps* Op, unsigned long database_length)
- {
- // if the database is empty, display an error statement
- if (database_length == 0) {
- cout << "The database is empty, No elements in the database" << endl;
- }
- // if the database is not empty, display all the elements in the database
- else {
- // display a title
- cout << "The database are: " << endl << endl;
- // display the elements
- for (unsigned long i=0;i<database_length;i++) {
- cout << "Operational Amplifier: " << Op[i].Name <<endl;
- cout << "Number of pins : " << Op[i].PinCount << endl;
- cout << "Slew Rate : " << Op[i].SlewRate << " Volts/microsecond" << endl << endl;
- }
- }
- }