Link to home
Start Free TrialLog in
Avatar of stallion5000
stallion5000

asked on

how do I do math calculations from within a class?

I need to do math claculations from within a class and then display it to the screen after every instance of that call. Take a look at how I have it, I'm getting all sorts of errors right now. Take a look, I have comments by where I think the problems are.

#include<iostream>
#include<string>
#include<fstream>
#include<iomanip>

using namespace std;




class theShares
{
private:
      string name;
      string shares;
      string tClose;
      string high;
      string low;
      string pClose;
      double gain;


public:


      void setname(string theName)
      {
            name=theName;
      }

      void setshares(string theShares)
      {
            shares=theShares;
      }

      void settClose(string dayClose)
      {
            tClose=dayClose;
      }

      void sethigh(string theHigh)
      {
            high=theHigh;
      }

      void setlow(string theLow)
      {
            low=theLow;
      }

      void setpClose(string oldClose)
      {
            pClose=oldClose;
      }

      void setgain(double pGain)        //HERE I'M SETTING THE VOID
      {
            gain=pGain;
      }

      double getgain()
      {
            return (tClose - pClose) / pClose * 100;      //AND THEN HERE IS WHERE I'M DOING THE CLACULATIONS
      }

      string getname()
      {
            return name;
      }

      string getshares()
      {
            return shares;
      }

      string gettClose()
      {
            return tClose;
      }

      string gethigh()
      {
            return high;
      }

      string getlow()
      {
            return low;
      }

      string getpClose()
      {
            return pClose;
      }

      friend istream &operator >> (istream &stream, theShares &myShares)
      {
            
            stream >> myShares.name;
            stream >> myShares.shares;
            stream >> myShares.tClose;
            stream >> myShares.high;
            stream >> myShares.low;
            stream >> myShares.pClose;
            stream.get();
      
            return stream;
      }

      friend ostream &operator << (ostream &stream, theShares &myShares)
      {
            
            
            cout << "stock Name:  ";
            stream << myShares.getname() ;
            
            cout <<  "\n num of shares:  ";
            stream << myShares.getshares();

            cout << "\ntoday close:   ";
            stream << myShares.gettClose();

            cout << "\nthe high:  ";
            stream << myShares.gethigh();

            cout << "\nthe low:  ";
            stream << myShares.getlow();

            cout << "\nprevious close:  ";
            stream << myShares.getpClose();

            cout << "\npercent gain:  ";
            stream << myShares.getgain();          //AND FINALLY HERE IS WHERE I NEED IT TO GO TO THE SCREEN.

            
            



            return stream;
      }
      
      };


void main()
{
      
      double number = 100;
      double gain;
      theShares myShares;

      ifstream inFile("c:\\teststock.txt");

            while(!inFile.eof())
            {
                  inFile >> myShares;

                  
            

                  cout << myShares;
                  
            
            }

      

      
}


ASKER CERTIFIED SOLUTION
Avatar of rcarlan
rcarlan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of stallion5000
stallion5000

ASKER

Ok, good stuff. I remember strtod, but I'm not sure I'm using it right. Here is what I did, I did my calculations in main. I'm not sure that was a good move, it probably is not because I'm getting errors, but take a look and let me know what you think

#include<iostream>
#include<string>
#include<fstream>
#include<iomanip>

using namespace std;




class theShares
{
private:
      string name;
      string shares;
      string tClose;
      string high;
      string low;
      string pClose;
      


public:


      void setname(string theName)
      {
            name=theName;
      }

      void setshares(string theShares)
      {
            shares=theShares;
      }

      void settClose(string dayClose)
      {
            tClose=dayClose;
      }

      void sethigh(string theHigh)
      {
            high=theHigh;
      }

      void setlow(string theLow)
      {
            low=theLow;
      }

      void setpClose(string oldClose)
      {
            pClose=oldClose;
      }

      

      string getname()
      {
            return name;
      }

      string getshares()
      {
            return shares;
      }

      string gettClose()
      {
            return tClose;
      }

      string gethigh()
      {
            return high;
      }

      string getlow()
      {
            return low;
      }

      string getpClose()
      {
            return pClose;
      }

      friend istream &operator >> (istream &stream, theShares &myShares)
      {
            
            stream >> myShares.name;
            stream >> myShares.shares;
            stream >> myShares.tClose;
            stream >> myShares.high;
            stream >> myShares.low;
            stream >> myShares.pClose;
            stream.get();
      
            return stream;
      }

      friend ostream &operator << (ostream &stream, theShares &myShares)
      {
            
            
            cout << "stock Name:  ";
            stream << myShares.getname() ;
            
            cout <<  "\n num of shares:  ";
            stream << myShares.getshares();

            cout << "\ntoday close:   ";
            stream << myShares.gettClose();

            cout << "\nthe high:  ";
            stream << myShares.gethigh();

            cout << "\nthe low:  ";
            stream << myShares.getlow();

            cout << "\nprevious close:  ";
            stream << myShares.getpClose();

            

            
            



            return stream;
      }
      
      };


void main()
{
      
      double number = 100;
      double gain, t, p;
      theShares myShares;

      ifstream inFile("c:\\teststock.txt");

            while(!inFile.eof())
            {
                  inFile >> myShares;

                  
            

                  cout << myShares;
                  char* stop;
                  t = strtod(tClose.c_str(), &stop);
                  p = strtod(pClose.c_str(), &stop);
                  gain = (t-p)/p*100;
                  cout << gain;

                  
            
            }

      

      
}


Never mind I got it. Thanks!