Question

The following code is for Chapter 13 Programming Exercise 21. I'm not sure what all is...

The following code is for Chapter 13 Programming Exercise 21. I'm not sure what all is wrong with the code written. I get errors about stockSym being private and some others after that.This was written in the Dev C++ software. Can someone help me figure out what is wrong with the code with notes of what was wrong to correct it?

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

using namespace std;

template <class stockType>
class listType
{
   public:
       bool checkEmpty() const;
       bool checkFull() const;
       int getLength() const;
       int getMaxSize() const;
       void sortList();
       void display() const;
       ~listType();
       void insertAtPosition(const stockType& item, int postion);
       listType( int listSize = 50);
      
   protected:
       int maxSize;
       int len;
       stockType *lis;
};

template <class stockType>
bool listType <stockType> :: checkFull() const
{
   return (len == maxSize);
}

template <class stockType>
listType <stockType> :: listType (int listSize)
{
   maxSize = listSize;
   len = 0;
   lis = new stockType [maxSize];
}

template <class stockType>
listType <stockType> :: ~listType()
{
   delete [] lis;
}

template <class stockType>
void listType <stockType> :: sortList()
{
   int di, dj;
   int min;
   stockType te;
  
   for (di = 0; di < len; di++)
   {
       min = di;
       for (dj = di; dj < len; ++dj)
       if (lis[dj] < lis[min])
       min = dj;
       te = lis[di];
       lis[di] = lis[min];
       lis[min] = te;
   }
}

template <class stockType>
void listType <stockType> :: display() const
{
   int i;
   for (i = 0; i < len; ++i)
   cout << lis[i];
   cout << endl;
}

template <class stockType>
void listType <stockType> :: insertAtPosition (const stockType& item, int position)
{
   assert (position >= 0 && position < maxSize);
   lis [position] = item;
   len++;
}

class stockListType
{
   private:
       string stockSym;
       double openPrice;
       double closePrice;
       double high;
       double low;
       int noOfShare;
       double prevClosePrice;
       static double totalAsset;
       double perGainLoss;
      
   public:
       void setStock(string stockSym, double openPrice, double closePrice, double high, double low, int noOfShare, double prevClosePrice)
       {
           this -> stockSym = stockSym;
           this -> openPrice = openPrice;
           this -> closePrice = closePrice;
           this -> high = high;
           this -> low = low;
           this -> noOfShare;
           this -> prevClosePrice = prevClosePrice;
       }
      
       void Display()
       {
           cout << "stockListType name is " << stockSym << endl;
       }
      
       void showPricesDiff()
       {
           cout << "Opening price of today was " << openPrice << ", closing price was " << closePrice << ", high price was" << high<< ", low price was" << low << "Previous closing price is" << prevClosePrice << endl;
       }
      
       void calcGainLoss()
       {
           perGainLoss = (closePrice - prevClosePrice) / prevClosePrice * 100;
       }
      
       void showNumOfShare()
       {
           cout << "Number of share is " << noOfShare << endl;
       }
      
       bool operator > (stockListType &ss)
       {
           return (this -> stockSym.compare(ss.stockSym) > 0);
       }
      
       bool operator >= (stockListType &ss)
       {
           return (this -> stockSym.compare(ss.stockSym) >= 0);
       }
      
       bool operator < (stockListType &ss)
       {
           return (this -> stockSym.compare(ss.stockSym) < 0);
       }
      
       bool operator <= (stockListType &ss)
       {
           return (this -> stockSym.compare(ss.stockSym) <= 0);
       }
      
       bool operator == (stockListType &ss)
       {
           return (this -> stockSym.compare(ss.stockSym) == 0);
       }
      
       friend ostream& operator << (ostream &out, stockListType);
      
       friend istream& operator >> (istream &in, stockListType &ss);
      
       static double getTotalAssets()
       {
           return totalAsset;
       }
};

istream& operator >> (istream &in, stockListType &ss)
{
   in >> ss.stockSym;
   in >> ss.openPrice;
   in >> ss.closePrice;
   in >> ss.high;
   in >> ss.low;
   in >> ss.prevClosePrice;
   in >> ss.noOfShare;
   cout << ss.totalAsset << endl;
   ss.totalAsset += ss.noOfShare * ss.closePrice;
   return in;
}

ostream& operator << (ostream &out, stockListType &ss)
{
   ss.calcGainLoss();
   out << setw(8) << left << ss.stockSym << right << fixed << setprecision(2) << setw(8) << ss.openPrice << fixed << setprecision(2) << setw(8) << ss.closePrice << fixed << setprecision(2) << setw(8) << ss.high << fixed << setprecision(2) << setw(8) << ss.low << fixed << setprecision(2) << setw(8) << ss.prevClosePrice << fixed << setprecision(2) << setw(8) << ss.perGainLoss << fixed << setprecision(2) << setw(12) << ss.noOfShare << endl;
   return out;
}

double stockListType :: totalAsset = 0.0;
int main()
{
   ifstream infile("StockData.txt");
   listType <stockListType > stockArray(50);
   stockListType localStock;
   int count = 0;
   if(!infile)
   {
       cout << "Unable to Open File StockData.txt, so Exiting the Program";
       return 0;
   }
   while (!infile.eof())
   {infile >> localStock;
   stockArray.insertAtPosition(localStock, count++);
   }
  
   stockArray.sortList();
   cout << "*************First Investors Heaven*************" << endl;
   cout << "*************Financial Report*************" << endl;
   cout << "stockListType Today Previous Percent" << endl;
   cout << "Symbol Open Close High Low Close Gain Volume" << endl;
   cout << "------ ---- ----- ---- --- ----- ---- ------" << endl;
   stockArray.display();
   cout << "Closing assets: $" << fixed << setprecision(2) << stockListType :: getTotalAssets() << endl;
  
   system("pause");
  
   return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

`Hey,

Note: Brother in case of any queries, just comment in box I would be very happy to assist all your queries

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

using namespace std;

class stockListType
{
public:
string stockSym;
double openPrice;
double closePrice;
double high;
double low;
int noOfShare;
double prevClosePrice;
static double totalAsset;
double perGainLoss;
  
public:
void setStock(string stockSym, double openPrice, double closePrice, double high, double low, int noOfShare, double prevClosePrice)
{
this -> stockSym = stockSym;
this -> openPrice = openPrice;
this -> closePrice = closePrice;
this -> high = high;
this -> low = low;
this -> noOfShare;
this -> prevClosePrice = prevClosePrice;
}
  
void Display()
{
cout << "stockListType name is " << stockSym << endl;
}
  
void showPricesDiff()
{
cout << "Opening price of today was " << openPrice << ", closing price was " << closePrice << ", high price was" << high<< ", low price was" << low << "Previous closing price is" << prevClosePrice << endl;
}
  
void calcGainLoss()
{
perGainLoss = (closePrice - prevClosePrice) / prevClosePrice * 100;
}
  
void showNumOfShare()
{
cout << "Number of share is " << noOfShare << endl;
}
  
bool operator > (stockListType &ss)
{
return (this -> stockSym.compare(ss.stockSym) > 0);
}
  
bool operator >= (stockListType &ss)
{
return (this -> stockSym.compare(ss.stockSym) >= 0);
}
  
bool operator < (stockListType &ss)
{
return (this -> stockSym.compare(ss.stockSym) < 0);
}
  
bool operator <= (stockListType &ss)
{
return (this -> stockSym.compare(ss.stockSym) <= 0);
}
  
bool operator == (stockListType &ss)
{
return (this -> stockSym.compare(ss.stockSym) == 0);
}
  
friend ostream& operator << (ostream &out, stockListType);
  
friend istream& operator >> (istream &in, stockListType &ss);
static double getTotalAssets()
{
return totalAsset;
}
};

istream& operator >> (istream &in, stockListType &ss)
{
in >> ss.stockSym;
in >> ss.openPrice;
in >> ss.closePrice;
in >> ss.high;
in >> ss.low;
in >> ss.prevClosePrice;
in >> ss.noOfShare;
cout << ss.totalAsset << endl;
ss.totalAsset += ss.noOfShare * ss.closePrice;
return in;
}

ostream& operator << (ostream &out, stockListType &ss)
{
ss.calcGainLoss();
out << setw(8) << left << ss.stockSym << right << fixed << setprecision(2) << setw(8) << ss.openPrice << fixed << setprecision(2) << setw(8) << ss.closePrice << fixed << setprecision(2) << setw(8) << ss.high << fixed << setprecision(2) << setw(8) << ss.low << fixed << setprecision(2) << setw(8) << ss.prevClosePrice << fixed << setprecision(2) << setw(8) << ss.perGainLoss << fixed << setprecision(2) << setw(12) << ss.noOfShare << endl;
return out;
}

template <class stockType>
class listType
{
public:
bool checkEmpty() const;
bool checkFull() const;
int getLength() const;
int getMaxSize() const;
void sortList();
void display() const;
~listType();
void insertAtPosition(const stockType& item, int postion);
listType( int listSize = 50);
  
protected:
int maxSize;
int len;
stockType *lis;
};

template <class stockType>
bool listType <stockType> :: checkFull() const
{
return (len == maxSize);
}

template <class stockType>
listType <stockType> :: listType (int listSize)
{
maxSize = listSize;
len = 0;
lis = new stockType [maxSize];
}

template <class stockType>
listType <stockType> :: ~listType()
{
delete [] lis;
}

template <class stockType>
void listType <stockType> :: sortList()
{
int di, dj;
int min;
stockType te;
  
for (di = 0; di < len; di++)
{
min = di;
for (dj = di; dj < len; ++dj)
if (lis[dj] < lis[min])
min = dj;
te = lis[di];
lis[di] = lis[min];
lis[min] = te;
}
}

template <class stockType>
void listType <stockType> :: display() const
{
int i;
for (i = 0; i < len; ++i)
{
lis[i].calcGainLoss();
cout << setw(8) << left << lis[i].stockSym << right << fixed << setprecision(2) << setw(8) << lis[i].openPrice << fixed << setprecision(2) << setw(8) << lis[i].closePrice << fixed << setprecision(2) << setw(8) << lis[i].high << fixed << setprecision(2) << setw(8) << lis[i].low << fixed << setprecision(2) << setw(8) << lis[i].prevClosePrice << fixed << setprecision(2) << setw(8) << lis[i].perGainLoss << fixed << setprecision(2) << setw(12) << lis[i].noOfShare << endl;

}
cout << endl;
}

template <class stockType>
void listType <stockType> :: insertAtPosition (const stockType& item, int position)
{
assert (position >= 0 && position < maxSize);
lis [position] = item;
len++;
}

double stockListType :: totalAsset = 0.0;
int main()
{
ifstream infile("StockData.txt");
listType <stockListType > stockArray(50);
stockListType localStock;
int count = 0;
if(!infile)
{
cout << "Unable to Open File StockData.txt, so Exiting the Program";
return 0;
}
while (!infile.eof())
{infile >> localStock;
stockArray.insertAtPosition(localStock, count++);
}
  
stockArray.sortList();
cout << "*************First Investors Heaven*************" << endl;
cout << "*************Financial Report*************" << endl;
cout << "stockListType Today Previous Percent" << endl;
cout << "Symbol Open Close High Low Close Gain Volume" << endl;
cout << "------ ---- ----- ---- --- ----- ---- ------" << endl;
stockArray.display();
cout << "Closing assets: $" << fixed << setprecision(2) << stockListType :: getTotalAssets() << endl;
  
system("pause");
  
return 0;
}

Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
The following code is for Chapter 13 Programming Exercise 21. I'm not sure what all is...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Task: Tasks to complete: ------------------------------------------------------------------------------------------------------------------------------------------ given code: --------------------...

    Task: Tasks to complete: ------------------------------------------------------------------------------------------------------------------------------------------ given code: ------------------------------------------------------------------------------------------------------------------------------------------ main.cpp #include #include "rectangleType.h" using namespace std; // part e int main() { rectangleType rectangle1(10, 5); rectangleType rectangle2(8, 7); rectangleType rectangle3; rectangleType rectangle4; cout << "rectangle1: " << rectangle1 << endl; cout << "rectangle2: " << rectangle2 << endl; rectangle3 = rectangle1 + rectangle2;    cout << "rectangle3: " << rectangle3 << endl; rectangle4 = rectangle1 * rectangle2;    cout << "rectangle4: " << rectangle4 << endl; if (rectangle1 > rectangle2) cout << "Area...

  • #include <iostream> #include <string> #include "hashT.h" #include "stateData.h" using namespace std; void stateData::setStateInfo(string sName, string sCapital,...

    #include <iostream> #include <string> #include "hashT.h" #include "stateData.h" using namespace std; void stateData::setStateInfo(string sName, string sCapital,    double stArea, int yAdm, int oAdm) {    stateName = sName; stateCapital = sCapital; stArea = stateArea; yAdm = yearOfAdmission; oAdm = orderOfAdmission;       } void stateData::getStateInfo(string& sName, string& sCapital,    double& stArea, int& yAdm, int& oAdm) {    sName = stateName; sCapital = stateCapital; stArea = stateArea; yAdm = yearOfAdmission; oAdm = orderOfAdmission;       } string stateData::getStateName() { return stateName;...

  • I'm just not sure how to tackle all the template class this header wants me to...

    I'm just not sure how to tackle all the template class this header wants me to write. I got this far into making the template for public and private and would like help on the template functions. Thank you! This is in C++ by the way. #include <iostream> #include <cassert> using namespace std; #ifndef ARRAYLIST_H #define ARRAYLIST_H template<typename T> class arrayList { public:    arrayList(); //Constructor with default parameter. //Sets maxSize = 100 and length = 0 if no parameter...

  • C++ Redo Practice program 1 from chapter 11, but this time define the Money ADT class...

    C++ Redo Practice program 1 from chapter 11, but this time define the Money ADT class in separate files for the interface and implementation so that the implementation can be compiled separately from any application program. Submit three files: YourLastNameFirstNameInitialMoney.cpp - This file contains only the Money class implementation YourLastNameFirstNameInitialProj4.cpp - This file contains only the main function to use/test your money class YourLastNameFirstNameInitialMoney.h - This file contains the header file information.   Optional bonus (10%): Do some research on make...

  • Please help fix my code C++, I get 2 errors when running. The code should be...

    Please help fix my code C++, I get 2 errors when running. The code should be able to open this file: labdata.txt Pallet PAG PAG45982IB 737 4978 OAK Container AYF AYF23409AA 737 2209 LAS Container AAA AAA89023DL 737 5932 DFW Here is my code: #include <iostream> #include <string> #include <fstream> #include <vector> #include <cstdlib> #include <iomanip> using namespace std; const int MAXLOAD737 = 46000; const int MAXLOAD767 = 116000; class Cargo { protected: string uldtype; string abbreviation; string uldid; int...

  • A library maintains a collection of books. Books can be added to and deleted from and...

    A library maintains a collection of books. Books can be added to and deleted from and checked out and checked in to this collection. Title and author name identify a book. Each book object maintains a count of the number of copies available and the number of copies checked out. The number of copies must always be greater than or equal to zero. If the number of copies for a book goes to zero, it must be deleted from the...

  • Please zoom in so the pictures become high resolution. I need three files, FlashDrive.cpp, FlashDrive.h and...

    Please zoom in so the pictures become high resolution. I need three files, FlashDrive.cpp, FlashDrive.h and user_main.cpp. The programming language is C++. I will provide the Sample Test Driver Code as well as the codes given so far in text below. Sample Testing Driver Code: cs52::FlashDrive empty; cs52::FlashDrive drive1(10, 0, false); cs52::FlashDrive drive2(20, 0, false); drive1.plugIn(); drive1.formatDrive(); drive1.writeData(5); drive1.pullOut(); drive2.plugIn(); drive2.formatDrive(); drive2.writeData(2); drive2.pullOut(); cs52::FlashDrive combined = drive1 + drive2; // std::cout << "this drive's filled to " << combined.getUsed( )...

  • In this assignment you are required to complete a class for fractions. It stores the numerator...

    In this assignment you are required to complete a class for fractions. It stores the numerator and the denominator, with the lowest terms. It is able to communicate with iostreams by the operator << and >>. For more details, read the header file. //fraction.h #ifndef FRACTION_H #define FRACTION_H #include <iostream> class fraction { private: int _numerator, _denominator; int gcd(const int &, const int &) const; // If you don't need this method, just ignore it. void simp(); // To get...

  • - implement the Stack ADT using array – based approach. Use C++ program language #include "StackArray.h"...

    - implement the Stack ADT using array – based approach. Use C++ program language #include "StackArray.h" template <typename DataType> StackArray<DataType>::StackArray(int maxNumber) { } template <typename DataType> StackArray<DataType>::StackArray(const StackArray& other) { } template <typename DataType> StackArray<DataType>& StackArray<DataType>::operator=(const StackArray& other) { } template <typename DataType> StackArray<DataType>::~StackArray() { } template <typename DataType> void StackArray<DataType>::push(const DataType& newDataItem) throw (logic_error) { } template <typename DataType> DataType StackArray<DataType>::pop() throw (logic_error) { } template <typename DataType> void StackArray<DataType>::clear() { } template <typename DataType> bool StackArray<DataType>::isEmpty() const {...

  • This project is divided into 3 parts: Part 1. Create a new project and download the arrayList and...

    This project is divided into 3 parts: Part 1. Create a new project and download the arrayList and unorderedArrayList templates that are attached. Create a header file for your unorderedSet template and add it to the project. An implementation file will not be needed since the the new class will be a template. Override the definitions of insertAt, insertEnd, and replaceAt in the unorderedSet template definition. Implement the template member functions so that all they do is verify that the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT