?
Solved

how do I do math calculations from within a class?

Posted on 2005-03-27
3
Medium Priority
?
164 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month9 days, 6 hours left to enroll

764 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