473,396 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Ostream &operator to work in C++

1
I wrote a piece of code in C++ and then decided to change it to object-orientated and file should be achieved by
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
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string.h>
  4. #include <algorithm>
  5. #include <string>
  6. using namespace std;
  7.  
  8.  
  9. #define DATABASE_MAX 10
  10.  
  11.  
  12. #define DATABASE_FILENAME "database.txt"
  13.  
  14.  
  15. class OpAmps
  16. {
  17. private:
  18.     string Name;
  19.     unsigned int PinCount;
  20.     double SlewRate;
  21. public:
  22.     void Enter(OpAmps&, unsigned long&);
  23.     void Save(const OpAmps*, unsigned long);
  24.     void Load(OpAmps*, unsigned long&);
  25.     void Sort(OpAmps*, unsigned long);
  26.     void Display(const OpAmps*, unsigned long);
  27.  
  28.     friend bool SortName(const OpAmps &, const OpAmps &);
  29.     friend bool SortSlewRate(const OpAmps &, const OpAmps &);
  30. };
  31.  
  32.  
  33. int main()
  34. {
  35.   OpAmps OpAmp[DATABASE_MAX];        // the database
  36.   OpAmps Menu;
  37.   unsigned long database_length = 0;        // the number of elements in the database
  38.   char UserInput;
  39.  
  40. // loop until the user wishes to exit
  41. while (1) {
  42.  
  43.     // show the menu of options
  44.     cout << endl;
  45.     cout << "Op-amp database menu" << endl;
  46.     cout << "--------------------" << endl;
  47.     cout << "1. Enter a new op-amp into the database" << endl;
  48.     cout << "2. Save the database to disk" << endl;
  49.     cout << "3. Load the database from disk" << endl;
  50.     cout << "4. Sort the database" << endl;
  51.     cout << "5. Display the database" << endl;
  52.     cout << "6. Exit from the program" << endl << endl;
  53.  
  54.     // get the user's choice
  55.     cout << "Enter your option: ";
  56.     cin >> UserInput;
  57.     cout << endl;
  58.  
  59.     // act on the user's input
  60.     switch(UserInput) 
  61.     {
  62.         case '1':
  63.             Menu.Enter(OpAmp[database_length], database_length);
  64.         break;
  65.  
  66.         case '2':
  67.             Menu.Save(OpAmp, database_length);
  68.         break;
  69.  
  70.         case '3':
  71.             Menu.Load(OpAmp, database_length);
  72.         break;
  73.  
  74.         case '4':
  75.             Menu.Sort(OpAmp, database_length);
  76.         break;
  77.  
  78.         case '5':
  79.             Menu.Display(OpAmp, database_length);
  80.         break;
  81.  
  82.         case '6':
  83.         return 0;
  84.  
  85.         default:
  86.         cout << "Invalid entry" << endl << endl;
  87.         break;
  88.         }
  89.     }
  90. }
  91.  
  92.  
  93. void OpAmps::Enter(OpAmps& Op, unsigned long& database_length)
  94. {
  95.     // if the database is full, inform the user
  96.     if (database_length >= DATABASE_MAX) {
  97.         cout << "The database is full" << endl;
  98. }
  99.  
  100. // if the database is not full, get the data from the user and alter the database length
  101. else {
  102.     cout << "Add new data" << endl;
  103.     cout << "------------" << endl;
  104.     cout << "Enter Operational Amplifier name: ";
  105.     cin >> Op.Name;
  106.     cout << "Enter number of pins: ";
  107.     cin >> Op.PinCount;
  108.     cout << "Enter slew rate: ";
  109.     cin >> Op.SlewRate;
  110.     cout << endl;
  111.     cout << endl << "Operational Amplifier data is now in memory" << endl;
  112.     cout << endl << "Select option 1 from main menu to enter another data into the database" << endl;
  113.     cout << "select option 2 from main menu to save the database to the disk" << endl;
  114.     database_length++;
  115.     }
  116. }
  117.  
  118.  
  119. void OpAmps::Save(const OpAmps* Op, unsigned long database_length)
  120. {
  121.     fstream output_file;    // file stream for output
  122.  
  123.     // open the file
  124.     output_file.open(DATABASE_FILENAME, ios::out);
  125.  
  126.     // write length information to file
  127.     if(!output_file.good()) {
  128.       cout << "FATCH ERROR: Could not open file";
  129.       exit (1);
  130.     }
  131.     cout << "Opened file to save data to database" << endl << endl;
  132.  
  133.     // write length information to file
  134.     output_file << database_length << endl << endl;
  135.  
  136.     // write data to file
  137.     for (unsigned long i=0;i<database_length;i++) {
  138.         output_file << Op[i].Name << endl;
  139.         output_file << Op[i].PinCount << endl;
  140.         output_file << Op[i].SlewRate << endl << endl;
  141.         }
  142.  
  143.     if(database_length <= 10)  {
  144.         cout << database_length << " entry(s) added to database" << endl;
  145.         }
  146.         else
  147.         {
  148.         cout << "10 entry(s) added to database" << endl;
  149.         }
  150.  
  151.     // close the file
  152.     output_file.close();
  153. }
  154.  
  155.  
  156. void OpAmps::Load(OpAmps* Op, unsigned long& database_length)
  157. {
  158.     fstream input_file;        // file stream for input
  159.  
  160.     // open the file
  161.     input_file.open(DATABASE_FILENAME, ios::in);
  162.  
  163.     // load length information from file
  164.     if(!input_file.good()) {
  165.         cout << "FATCH ERROR: Could not open file";
  166.         exit (1);
  167.     }
  168.  
  169.     input_file >> database_length;
  170.  
  171.     if(database_length < 10) {
  172.         cout << "The data length now is: " << database_length << endl << endl;
  173.         cout << "Select option 5 from main menu to display the data loaded from disk" << endl;
  174.         }
  175.         else
  176.         {
  177.         cout << "The data length now is: 10" << endl << endl;
  178.         cout << "Select option 5 from main menu to display the data loaded from disk" << endl;
  179.     }
  180.  
  181.     // close the file
  182.     input_file.close();
  183. }
  184.  
  185.  
  186. void OpAmps::Sort(OpAmps* Op, unsigned long database_length)
  187. {
  188.     char UserInput;
  189.         cout << endl;
  190.         cout << "Sorting options" << endl;
  191.         cout << "---------------" << endl;
  192.         cout << "1. To sort by name" << endl;
  193.         cout << "2. To sort by slew rate" << endl;
  194.         cout << "3. No sorting" << endl << endl;
  195.  
  196.         // get the user's choice of sorting operation required
  197.         cout << "Enter your option: ";
  198.         cin >> UserInput;
  199.         cout << endl;
  200.  
  201.         // act on the user's input
  202.         switch(UserInput) {
  203.             case '1':
  204.             cout<<"sortName"<<endl;
  205.             std::sort(Op, Op + database_length, SortName);
  206.             cout << "The database has been sorted by name, select option 5 to show the database!" << endl << endl;
  207.             break;
  208.  
  209.             case '2':
  210.             cout<<"sortslew"<<endl;
  211.             std::sort(Op,Op + database_length, SortSlewRate);
  212.             cout << "The database has been sorted by Slew Rate, select option 5 to show the database!" << endl << endl;
  213.             break;
  214.  
  215.             case '3':
  216.             return;
  217.  
  218.             default:
  219.             cout << "Invalid entry" << endl << endl;
  220.             break;
  221.         }
  222.     }
  223.  
  224.  
  225. bool SortName(const OpAmps &First, const OpAmps &Second)
  226. {
  227.   return First.Name < Second.Name;
  228. }
  229.  
  230.  
  231. bool SortSlewRate (const OpAmps &First, const OpAmps &Second)
  232. {
  233.   return First.SlewRate < Second.SlewRate;
  234. }
  235.  
  236.  
  237. void OpAmps::Display(const OpAmps* Op, unsigned long database_length)
  238. {
  239.     // if the database is empty, display an error statement
  240.     if (database_length == 0) {
  241.         cout << "The database is empty, No elements in the database" << endl;
  242.     }
  243.     // if the database is not empty, display all the elements in the database
  244.     else {
  245.         // display a title
  246.         cout << "The database are: " << endl << endl;
  247.  
  248.         // display the elements
  249.         for (unsigned long i=0;i<database_length;i++) {
  250.             cout << "Operational Amplifier: "  << Op[i].Name <<endl;
  251.             cout << "Number of pins : " << Op[i].PinCount << endl;
  252.             cout << "Slew Rate : " << Op[i].SlewRate << " Volts/microsecond" << endl << endl;    
  253.         }
  254.     }
  255. }
Feb 8 '13 #1
0 1232

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Victor Irzak | last post by:
Hello, I have an ABC. it supports: ostream & operator << I also have a derived class that supports this operator. How can I call operator << of the base class for derived object??? Is it...
1
by: Tim Partridge | last post by:
I want operator<< to be a friend function of a class inside a namespace, but I don't know how. For example: #include <iostream> namespace ns { class foo { public: // there is something...
4
by: Rock | last post by:
I'm in the process of writing this program for complex numbers and I use DevC++. My professor on the other hand compiles on Borland 5.5. So I ocasionally save and run my work on Borland to see if...
2
by: tvn007 | last post by:
Please help why this code not compile I would like it to be something like this: istringstream stringstreams(line); stringstreams>>ptrstudent->bypassed>>ptrstudent->name>>ptrstudent->midterm;...
5
by: Matt | last post by:
I am trying to cast an ostream reference to void* and back again. The code below shows the problem isolated from a more complex program. It compiles quietly but seg faults upon execution. //...
2
by: waitan | last post by:
#include <algorithm> #include <iostream> #include <fstream> #include <string> #include <vector> #include <sstream> #include <iterator> #include <iomanip> using namespace std;
3
by: Nathan Sokalski | last post by:
I have a VB.NET function that I am using in an ASP.NET page. The code creates a String, which contains && (the JavaScript Logical AND operator) and is used as part of the JavaScript sent to the...
2
by: Henning Hasemann | last post by:
My class looks very exactly like this (left out boring things): class Situation { public: // ... virtual void toStream(std::ostream& ostr) const; }; std::string Situation::toString() const...
1
by: developereo | last post by:
Hi folks, Can somebody shed some light on this problem? class Interface { protected: Interface() { ...} virtual ~Interface() { ... } public:
4
by: nembo kid | last post by:
Was wondering why the function that overloads = operator, returns always a const reference to the class. Why the qualifier 'const'? Thanks in advance
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.