Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

how do I do math calculations from within a class?

Posted on 2005-03-27
3
Medium Priority
?
165 Views
Last Modified: 2010-04-24
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;
                  
            
            }

      

      
}


0
Comment
Question by:stallion5000
  • 2
3 Comments
 
LVL 9

Accepted Solution

by:
rcarlan earned 2000 total points
ID: 13640878
You cannot perform calculations with strings.
May I suggest you change the tClose and pClose data members to double.
You can also use strtod to parse a string and retrieve the number it represents.
0
 

Author Comment

by:stallion5000
ID: 13640937
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;

                  
            
            }

      

      
}


0
 

Author Comment

by:stallion5000
ID: 13641012
Never mind I got it. Thanks!
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month15 days, 21 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question