//There is a problem in int main when scanning in the constructors into the pointer array ( found around line 843)
//(sorts by the value generated in calcStockIndex — a method within the stock class)

#include <iostream>
#include <fstream>
#include<cmath>
using namespace std;

/*
 Purpose: To store all information that can be looked at for all financial markets
 */
class financialMarkets{

private:
    double risk;// a double which represents the standard deviation (inherent risk)
    double high; // the highest value the financial instrument has reached in the last year
    double low; // the lowest value the financial instrument has reached in the last year
    double meanValue; // the mean value of the highest and lowest value (midline)

public:    
    financialMarkets(double highPrice , double lowPrice){
        setHigh(highPrice); // set high price
        setLow(lowPrice); // set low price
    } 
private:    


protected:

     /* Purpose: To set the highest price the financial instrument has reached
     * Parameters: highPrice - the highest value the financial instrument has reached in the last year
     * Returns: none
     * Side-effects: sets highPrice value
     */
    virtual void setHigh(double highPrice){
        high = highPrice; 
    }
    /* Purpose: To set the lowest price the financial instrument has reached
     * Parameters: lowPrice - the lowest value the financial instrument has reached in the last year
     * Returns: none
     * Side-effects: sets lowestPrice value
     */
    virtual void setLow(double lowPrice){
        low = lowPrice; 
    }
public:
    /* Purpose: To get the risk of the stock
     * Parameters: none
     * Returns: risk - the standard deviation in a double value of the stock
     * Side-effects:none
     */
    virtual double getRisk(){ 
        calcRisk();
        return risk;
    }
    /* Purpose: To get the mean value of the stock
     * Parameters: none
     * Returns:  meanValue - the mean value of the stock
     * Side-effects: none
     */
    virtual double getMeanValue(){
        calcMeanValue();
        return meanValue;
    }
protected:
    /* Purpose: To calculate the mean value 
     * Parameters: none
     * Returns:  none
     * Side-effects: meanValue is set to the proper value
     */
    virtual void calcMeanValue(){  
        meanValue = (high + low)/2;
    }
     /* Purpose: To calculate the risk (standard deviation)
     * Parameters: none
     * Returns:  none
     * Side-effects: risk is now set to its correct value 
      */
    virtual void calcRisk(){
        int maxNumberOfSquares = 2;
        double square[maxNumberOfSquares];
        double stanDev = 0;

        square[0] = pow((high - meanValue),2); 
        square[1] = pow((low - meanValue),2); 
        risk = (square[0] + square[1])/2;
    }
};

/*
 Purpose: To store all information that should be analyzed for a stock
 */

class Stock{

private:

    double cashFlow;// the cash flow of the business for the annum
    double capEx; // The capital expenditure of the business for the annum
    double netIncome; // The net income of the business for the annum
    double revenue; // the revenue of the business for the annum
    double divPaid; // the dividends paid that annum
    double grossProfit; // the gross profit of the business for the annum
    double totalCapital; // the total capital of the business
    string companyName; // name of the company
    bool  Buffett;// a bool which return true if Buffett would approve of the stock
    // and false if he wouldn't approve of the stock based on certain criteria
    bool effective; // a bool which returns true or false based on whether the capital use
    //of the company is effective
    double stockPrice; // the stock price of the stock
    double PE; // the PE of the stock
    double bookValue; // the book value of the stock
   // financialMarkets finance;

public:

    /* Purpose: a stock constructor which i used to set variables through int main and functions
     * which are called within the constructor 
     * Parameters:  
     *      name: a string representing the name of the company
     *      CF: a double which represents the cashflow of the company
     *      capitalEx: a double which represent the capital expenditure of the company
     *      income: a double which represents the income of the company
     *      sales: a double representing the sales of the company
     *      divident: a double which represents the divident of the company
     *      gProfit: a double which represents the gross profit of the company
     *      capital: a double which represents the shareholder's capital of the company
     * Returns: none
     * Side-effects: sets of the variables listed above and checks to see if the doubles are valid

     */

    Stock(string name, double CF, double capitalEx, double income, double sales, 
            double divident, double gProfit, double capital, double price, double bValue){
        // call various check and set methods 
        setCompanyName(name); // set company name
        setDouble(CF, "cashflow"); // check cashFlow
        setCashFlow(CF);// // set cashFlow
        setDouble(capitalEx, "capital expenditure"); 
        setCapEx(capitalEx);
        setDouble(income, "net income");
        setIncome(income);
        setDouble(sales, "revenue");
        setRevenue(sales); 
        setDouble(divident, "divident");
        setDividend(divident); 
        setDouble(gProfit, "gross profit");
        setGrossProfit(gProfit); 
        setDouble(capital, "shareholder equity");
        setCapital(capital);
        setDouble(price, "stock price");
        setPrice(price);
        setDouble(bValue, "book value");
        setBookValue(bValue);
    }
private:
       /* Purpose: To check if the double is valid
     * Parameters: holder - a double which is used as a holder for the double which will be checked
     * type - a string representing the type of information this is i.e. cash flow, capital, etc. 
     * Returns: none
     * Side-effects: sets holder to zero if it is not a double
     */
    virtual void setDouble(double holder, string type){

        if(!(cin>>holder)){   
            cout << "An incorrect value has been entered for " << type << ".";
            cout << " Setting " << type << "to 0.";
            holder = 0;    
        }

    }
    /* Purpose: To set the company name
     * Parameters:  name - a string representing the company name 
     * Returns: none
     * Side-effects: sets companyName
     */
    virtual void setCompanyName(string name){

        companyName = name; 
    }
     /* Purpose: To set the cashflow
     * Parameters:  CF - a double which represents the cashflow of the company
     * Returns: none
     * Side-effects: sets cashflow
     */
    virtual void setCashFlow(double CF){

        cashFlow = CF; 
    }
    /* Purpose: To set the cap ex of the company
     * Parameters:  CF - a double which represents the cashflow of the company
     * Returns: none
     * Side-effects: sets cashFlow
     */
    virtual void setCapEx(double capitalEx){

        capEx = capitalEx; 
    }
    /* Purpose: To set the income of the company
     * Parameters:  income - the income of the company
     * Returns: none
     * Side-effects: sets the netIncome
     */
    virtual void setIncome(double income){
        netIncome = income;
    }
    /* Purpose: To set the revenue of the company
     * Parameters:  sales - a double which represents the sales generated by the company
     * Returns: none
     * Side-effects: sets the 'revenue' of the company to the proper value
     */ 
    virtual void setRevenue(double sales){
        revenue = sales; 
    }
     /* Purpose: To set the dividend paid by the company
     * Parameters: div - a double representing the dividend of the comapny
     * Returns: none
     * Side-effects: sets the divPaid of the company to the proper value
     */ 
    virtual void setDividend(double div){
        divPaid = div;
    }
    /* Purpose: To set the gross profit 
     * Parameters: div - a double representing the dividend of the company
     * Returns: none
     * Side-effects: sets the grossProfit
     */ 
    virtual void setGrossProfit(double gProfit){
        grossProfit = gProfit; 
    }
     /* Purpose: To set the capital of the company
     * Parameters: capital - a double representing the capital of the company
     * Returns: none
     * Side-effects: sets totalCapital variable to proper value
     */ 
    virtual void setCapital(double capital){

        totalCapital = capital; 
    }
     /* Purpose: To set the price of the company stock
     * Parameters: price - a double representing the price of the stock
     * Returns: none
     * Side-effects: sets the stockPrice variable to the proper variable
     */ 
    virtual void setPrice(double price){
        stockPrice = price;
    }
    /* Purpose: To set the book value of the stock to the proper value
     * Parameters: bValue- a double representing the book value of the company
     * Returns: none
     * Side-effects: sets the bookValue to the correct value
     */ 
    virtual void setBookValue(double bValue){
        bookValue = bValue;

    }
    /* Purpose: To calculate the PE Ratio
     * Parameters: none
     * Returns: none
     * Side-effects: sets the PE value
     */ 
    virtual void calcPERatio(){

        PE = stockPrice/netIncome; 
    }
    /* Purpose: To calculate the capital expenditure of the company
     * Parameters: non
     * Returns: capExRatio - a double representing the capital expenditure ratio of the comapny
     * Side-effects: none
     */ 
    virtual double calcCapEx(){
        double capExRatio = 0; 
        capExRatio = (cashFlow/capEx) * 100;
        return capExRatio;
    }

     /* Purpose: To calculate the gross profit percentage of the company
     * Parameters: none
     * Returns: gProfitPercentage - a double representing the percentage of revenue represented by gross profit
     * Side-effects: none
     */
    virtual double calcGrossProfitPercentage(){

        double gProfitPercent;

        gProfitPercent = (grossProfit/revenue)*100; 

        return gProfitPercent; 

    }
   /* Purpose: To calculate the net profit/profit percentage of the compay
     * Parameters: none
     * Returns: profit - a double representing percentage of revenue turned into profit
     * Side-effects: none
     */
    virtual double calcNetProfit(){

        double profit;

        profit = (netIncome/revenue)* 100; 

        return profit;   
    }
    /* Purpose: To calculate the return on equity
     * Parameters: none
     * Returns: ROE - a double representing the return on equity of the company
     * Side-effects: none
     */
    virtual double calcROE(){

        double ROE;

        ROE = (netIncome/totalCapital)*100;

        return ROE;

    }
     /* Purpose: To calculate return on invested capital
     * Parameters: none
     * Returns: ROIC - a double representing the return on invested capital
     * Side-effects: none
     */
    virtual double calcROIC(){

        double ROIC;

        ROIC = (netIncome - divPaid)/totalCapital;

        return ROIC;
    }
    /* Purpose: To calculate dividend yield 
     * Parameters: none
     * Returns:  divYeild- a double representing the return on invested capital
     * Side-effects: none
     */
    virtual double calcDivYeild(){

        double divYeild;

        divYeild = (divPaid/stockPrice)*100;
        return divYeild; 
    }
     /* Purpose: To determine if buffett will approve of the stock
     * Parameters: none
     * Returns: none
     * Side-effects: sets Buffett to true or false depending on whether Buffett would approve of it 
     */
    virtual void buffettAnalysis(){

        Buffett = false;

        if(calcCapEx() <= 50){
            if(calcGrossProfitPercentage() >= 40){
                if(calcNetProfit() >= 20){
                    if(calcROE() >= 15){
                        Buffett= true;
                    }
                }    
            }   
        }   
    }
    /* Purpose: To determine if the capital use is effective
     * Parameters: none
     * Returns: none
     * Side-effects: sets effective to true or false 
     */
    virtual void capitalUse(){  
        effective = false; 

        if(calcROIC() >= 15){
            if(calcROE() >= 15){
                effective = true; 
            }  
        }
    }

protected:
    /* Purpose: To get the Buffett variable value
     * Parameters: none
     * Returns: Buffett - a bool which is true or false depending on whether it follows criteria
     * Side-effects: technically it runs the buffettAnalysis function in which sets Buffett to true or false
     */
    virtual bool getBuffett(){
        buffettAnalysis();
        return Buffett;
    }
     /* Purpose: To get the effective variable 
     * Parameters: none
     * Returns: Effective - returns the effective value 
     * Side-effects: technically it runs the buffettAnalysis function in which sets Buffett to true or false
     */
    virtual bool getEffective(){
        capitalUse();
        return effective; 
    } 
    virtual double getPERatio(){ 
        calcPERatio();   
        return PE;
    }
    virtual double getDivYeild(){
        return calcDivYeild();
    }

  public:  

    virtual double calcStockIndex() = 0; 
 /*    
    virtual void sort(double stockPrices[], int numOfStocks ){
        int smallestIndex = 0;
        int compareIndex = 0;
        int tmp = 0;
        for (compareIndex = smallestIndex + 1; compareIndex < numOfStocks ; compareIndex++) {
                if (array[smallestIndex] > array[compareIndex]) {
                    tmp = array[smallestIndex];
                    array[smallestIndex] = array[compareIndex];
                    array[compareIndex] = tmp;
                }
            }

    }   */
    virtual void printReport(int index){

        //cout << "Ranking" << "   Company Name" << "  Buffett Approval" << "   Effective Capital Use " << "  P/E"  << "   DivYeild" << "    Risk    "<< "   Mean Value" <<  "  Index Rating" << endl;
       // cout << endl; 
            cout<<index << ". " << companyName;
            if(Buffett){
                cout << "Yes    ";
            }
            if(Buffett == 0){
                cout<< "No      ";
            }
            if(effective){
                cout<< "Yes     ";
            }
            if(effective == 0){
                cout << "No     ";
            }
            cout<< PE << "      ";
            cout<< getDivYeild() << "     ";
          //  cout << finance.getRisk() << "     ";
          //  cout << finance.getMeanValue()<< "    ";
            cout<< calcStockIndex()<< "      " << endl;      
    }

};

class ConsumerGoods : public Stock{

private:

    double marketPE;
    double marketAvDivYeild; // represents the average market divident
    double PE;
    double divPaid;
    double bookValue;
    double stockIndex;

public:  
    ConsumerGoods(string name, double CF, double capitalEx, 
    double income, double sales, double divident, double gProfit, double capital, double price , double bValue
): Stock(name, CF, capitalEx,
    income, sales, divident, gProfit, capital, price, bValue){ 
        setMarketPE(14.72);
        setMarketAvDivYeild(3.29);
        setDivPaid(divident);
        setBookValue(bValue);
    }

protected:    
    virtual void setMarketPE(double mPE){
        marketPE = mPE;
    } 
    virtual void setMarketAvDivYeild(double mDiv){
        marketAvDivYeild = mDiv; 

    }
    virtual void setDivPaid(double div){
        divPaid = div; 
    }
    virtual void setBookValue(double bValue){ 
        bookValue = bValue; 
    }

    virtual double calcStockIndex(){ 
        double stockIndex; 
        stockIndex = sqrt(marketPE * marketAvDivYeild * bookValue  * divPaid);
        return stockIndex; 
    }
};

class Retail : public ConsumerGoods{
public:
    Retail(string name, double CF, double capitalEx, 
    double income, double sales, double divident, double gProfit, double capital, double price , double bValue): ConsumerGoods(name, CF, capitalEx,
    income, sales, divident, gProfit, capital, price, bValue){

        setMarketPE(33.13); 
        setMarketAvDivYeild(1.92);
    }

    virtual void printSector(){
        cout<< "Retail";
    }
};

class Hotel : public ConsumerGoods{
public:
    Hotel(string name, double CF, double capitalEx, 
    double income, double sales, double divident, double gProfit, double capital, double price , double bValue): ConsumerGoods(name, CF, capitalEx,
    income, sales, divident, gProfit, capital, price, bValue){
        setMarketPE(30.03);
        setMarketAvDivYeild(1.42);
    }

    virtual void printSector(){
        cout<< "Hotel";
    }
};

class Food : public ConsumerGoods{
public:   
    Food(string name, double CF, double capitalEx, 
    double income, double sales, double divident, double gProfit, double capital, double price , double bValue 
     ): ConsumerGoods(name, CF, capitalEx,
    income, sales, divident, gProfit, capital, price, bValue){
        setMarketPE(41.30);
        setMarketAvDivYeild(2.49);
    }

    virtual void printSector(){
        cout<< "Food";
    }  
};

class Technology : public Stock{

private: 
    double marketPE;
    double marketROE;
    double divPaid; 
    double bookValue; 
    double stockIndex; 

public:
    Technology(string name, double CF, double capitalEx, 
    double income, double sales, double divident, double gProfit, double capital, double price , double bValue 
     ):Stock(name, CF, capitalEx,
    income, sales, divident, gProfit, capital, price, bValue){
        setMarketPE(32);
        setMarketAvROE(12);
        setDivPaid(divident);
        setBookValue(bValue);
    }

protected:    
    virtual void setMarketPE(double mPE){
        marketPE = mPE;
    } 
    virtual void setMarketAvROE(double mROE){
        marketROE = mROE; 
    }
    virtual void setDivPaid(double div){
        divPaid = div; 
    }
    virtual void setBookValue(double bValue){
        bookValue = bValue; 
    }
public:    

    virtual double calcStockIndex(){ 
        double stockIndex; 
        stockIndex = sqrt( marketPE * (1+marketROE) * bookValue  * divPaid);
        return stockIndex; 
    }


};

class InfoTechnology: public Technology{
public:    
    InfoTechnology(string name, double CF, double capitalEx, 
    double income, double sales, double divident, double gProfit, double capital, double price , double bValue 
      ): Technology(name, CF, capitalEx,
    income, sales, divident, gProfit, capital, price, bValue){
        setMarketPE(20.9);
        setMarketAvROE(32.00);
    }
protected:
    virtual void printSector(){
        cout << "Information Technology";
    }
};


class Electronics : public Technology{
public:
    Electronics(string name, double CF, double capitalEx, 
    double income, double sales, double divident, double gProfit, double capital, double price , double bValue 
     ): Technology(name, CF, capitalEx,
    income, sales, divident, gProfit, capital, price, bValue){
        setMarketPE(7.9);
        setMarketAvROE(28.00);
    }
protected:
    virtual void printSector(){
        cout << "Electronics";
    }

};

class Services : public Stock{

private:
    double marketPE;
    double divPaid;
    double bookValue; 
    double stockIndex;
    double stockPrice;

public:

    Services(string name, double CF, double capitalEx, 
    double income, double sales, double divident, double gProfit, double capital, double price , double bValue):Stock(name, CF, capitalEx,
    income, sales, divident, gProfit, capital, price, bValue){
        setMarketPE(33.13);
        setDivPaid(divident);
        setBookValue(bValue);
        setStockPrice(price);
    }

protected:    
    virtual void setMarketPE(double mPE){
        marketPE = mPE;
    } 
    virtual void setDivPaid(double div){
        divPaid = div; 
    }
    virtual void setBookValue(double bValue){
        bookValue = bValue; 
    }
    virtual void setStockPrice(double price){
        stockPrice = price; 
    }
public:
    virtual double calcStockIndex(){ 
        double stockIndex; 
        stockIndex = sqrt( marketPE * stockPrice * bookValue  * divPaid);
        return stockIndex; 
    }
};

class Entertainment : public Services{
public:
    Entertainment(string name, double CF, double capitalEx, 
    double income, double sales, double divident, double gProfit, double capital, double price , double bValue): Services(name, CF, capitalEx,
    income, sales, divident, gProfit, capital, price, bValue){
        setMarketPE(23);
    }
protected:
    virtual void printSector(){
        cout << "Entertainment";
    }

};


class Air : public Services{
public:
    Air(string name, double CF, double capitalEx, 
    double income, double sales, double divident, double gProfit, double capital, double price , double bValue): Services(name, CF, capitalEx,
    income, sales, divident, gProfit, capital, price, bValue){
        setMarketPE(10.4);
    }
protected:
    virtual void printSector(){
        cout << "Airline";
    }

};

class Commodities{
private:
    double meanValue; 
    double risk;
    double factor; 
   // financialMarkets finance;

public:    
    Commodities(){

        setRisk();
        setMeanValue();
        setFactor(1);
    }
protected:   
    virtual void setFactor(double multiple){

        factor = multiple;
    }

    virtual void setMeanValue(){

     //   meanValue = finance.getMeanValue(); 
    }

    virtual void setRisk(){
   //     risk = finance.getRisk();
    }

    virtual double calcCommodityFactor(){

        double commodityFactor; 

     //   commodityFactor = (meanValue + risk) * factor;

        return commodityFactor;  
    }  

    virtual void sort(int numOfC, double array[]){

        int smallestIndex = 0;
        int compareIndex = 0;
        int tmp = 0;

        for (smallestIndex = 0; smallestIndex < numOfC - 1; smallestIndex++) {
            for (compareIndex = smallestIndex + 1; compareIndex < numOfC ; compareIndex++) {

                if (array[smallestIndex] > array[compareIndex]) {
                    tmp = array[smallestIndex];
                    array[smallestIndex] = array[compareIndex];
                    array[compareIndex] = tmp;
                }
            }
        }
    } 
public:    
    virtual void printCommodity();
    virtual void printReport(int numOfC){

        int index = 0; 

        for(index = 0; index <= numOfC; index++){
            cout<< "Commodity     Risk      Mean       Commodity Factor" << endl; 
      //      printCommodity(); cout << risk << meanValue << calcCommodityFactor() << endl;
        }        
    }
};
class Gold : public Commodities{
    Gold(){
        setFactor(0.95);
    }
    virtual void printCommodity(){ 
        cout << "Gold";
    }
};

class Oil : public Commodities{
    Oil(){
        setFactor(0.86);
    }   
    virtual void printCommodity(){
        cout << "Oil";
    }
};


int main(int argc, char** argv) {

   string junk;
   string fileName;
   string financialInstrument;
   string sector; 
   string companyName;
   double capEx = 0;
   double cashFlow = 0;
   double income =0; 
   double sales = 0;
   double divident = 0;
   double gProfit = 0; 
   double capital = 0; 
   double price= 0;
   double bValue = 0;
   const int maxNum = 100;
   int numOfStocks = 0; 
   Stock *stocks[maxNum];
   //Commodities *commodities[maxNum];
   //financialMarkets *finance;
   ifstream inputFile; // C++ object that represents a file
   int index = 0;
   double highPrice;
   double lowPrice;

   // get the name of the data file

   cout << "Please enter the name of the data file: ";
   getline(cin, fileName );
   // open the file for reading
   inputFile.open(fileName ); 

   // process the data file to read in an employee's information, and create
   // the appropriate employees to store within our array.

   if(!inputFile){
       cout << "Input file has failed. Please check input file!";

   }
      getline(inputFile, financialInstrument);

   if(financialInstrument == "Stock"){
        getline( inputFile, sector);
         while (sector!= "END"){
            if(numOfStocks == maxNum ){// If there are more stocks than the set maximum
                cout<<"Error: There are too many stocks. Processing the first 100. Check the data file."<<endl;
            }   
            getline(inputFile, companyName);
            inputFile>> cashFlow;
            getline( inputFile, junk );
            inputFile >> capEx;
            getline( inputFile, junk );
            inputFile >> income;
            getline( inputFile, junk );
            inputFile >> sales;
            getline( inputFile, junk );
            inputFile >> divident;
            getline( inputFile, junk );
            inputFile >> gProfit; 
            getline( inputFile, junk );
            inputFile >> capital; 
            getline( inputFile, junk );
            inputFile >> price;
            getline( inputFile, junk );
            inputFile >> bValue;
            getline( inputFile, junk );
            inputFile >> highPrice; 
            getline( inputFile, junk );
            inputFile >> lowPrice;
            getline( inputFile, junk );

            if (sector == "Food" ){
               stocks[numOfStocks] = new Food(companyName, cashFlow, capEx, income, sales, divident, gProfit, capital, price, bValue);
            } else if ( sector == "Entertainment" ) {
                stocks[numOfStocks] = new Entertainment( companyName, cashFlow, capEx, income, sales, divident, gProfit, capital, price, bValue );
            } else if ( sector == "Electronics" ) {
                stocks[numOfStocks] = new Entertainment( companyName, cashFlow, capEx, income, sales, divident, gProfit, capital, price, bValue );
            }
             else if ( sector == "Information Technology") {
                 stocks[numOfStocks] = new InfoTechnology( companyName, cashFlow, capEx, income, sales, divident, gProfit, capital, price, bValue );
            } else if ( sector == "Air" ) {
                stocks[numOfStocks] = new Entertainment( companyName, cashFlow, capEx, income, sales, divident, gProfit, capital, price, bValue );
            }else if ( sector == "Hotel" ) {
                stocks[numOfStocks] = new Hotel( companyName, cashFlow, capEx, income, sales, divident, gProfit, capital, price, bValue );
            }else if ( sector == "Retail" ) {
                stocks[numOfStocks] = new Retail( companyName, cashFlow, capEx, income, sales, divident, gProfit, capital, price, bValue );
            }
            numOfStocks++;
            getline(inputFile, sector);
            cout <<"loop run";
        }   
       /*
        Stock *tmp;
         double temp;
         int counter;
            for(counter = 0; counter < numOfStocks; counter++) {
        temp = stocks[counter]->calcStockIndex();
                    if(temp < stocks[counter+1]->calcStockIndex()) {
                        stocks[counter] = stocks[counter+1];
                        stocks[counter+1] = tmp;
            counter = 0;
                  }
            }


        cout << "Ranking" << "   Company Name" << "  Buffett Approval" << "   Effective Capital Use " << "  P/E"  << "   DivYeild" << "    Risk    "<< "   Mean Value" <<  "  Index Rating" << endl;
        cout << endl; 
        for(index = 0; index < numOfStocks;index++){
            cout << "hello";
            //stocks[index]->printReport(index);   
        }  
        */
   }            


    return 0;
}

Dani AI

Generated

Most likely causes of the crash/hang when the program starts allocating into the stocks pointer array (the region described by ) are easy to pinpoint from the posted code: a validation helper that reads from the wrong stream, an out‑of‑bounds write, a few ctor/instantiation typos, and some uninitialized members that are later read. is right that the file is large, so here are focused, high‑value fixes and quick diagnostic steps.

  1. Stream/validation bug (very likely blocker)
  • setDouble currently does cin >> holder while the program is reading from inputFile. That will block/wait for console input and also does not change the caller’s variable because holder is passed by value. Replace validation so it does not read from cin; validate the value that was already read from the file or test the file stream state after extraction.
  1. Array bounds
  • The code checks if (numOfStocks == maxNum) but still writes to stocks[numOfStocks] and then numOfStocks++. Use >= or break/skip to avoid writing at index maxNum (valid indices are 0..maxNum-1).
  1. Wrong class instantiation and uninitialized state
  • Several sector branches allocate the wrong derived type (e.g., Electronics/Air branches use Entertainment). Fix the mapping so each sector creates the correct derived object.
  • Initialize Buffett and effective (either in the constructor initializer list or by calling buffettAnalysis() / capitalUse() before printing), and use getter methods (getPERatio(), getDivYeild(), getBuffett(), getEffective()) in printReport() so computed values are used.
  1. Safety and modern best practices
  • Add a virtual destructor to Stock (and other polymorphic bases):
    virtual ~Stock() = default;
  • Use stronger diagnostics: compile with -Wall -Wextra and run under AddressSanitizer (-fsanitize=address,undefined) or valgrind to catch out‑of‑bounds and use‑of‑uninitialized.
  • Replace the raw C array with std::vector<std::unique_ptr<Stock>> and use a small factory (map from sector string → lambda) to avoid long if/else chains.

Minimal example fixes (conceptual):

void setDouble(double &val, const std::string &type) {
  if (!std::isfinite(val)) { std::cerr<<"Bad "<<type<<", using 0\n"; val = 0; }
}

if (numOfStocks >= maxNum) { cerr<<"Too many stocks\n"; break; }

Quick debug recipe: add a logging print before each allocation (print sector and numOfStocks), run with sanitizers, and inspect the exact input file lines around the failure — mismatched sector strings or blank lines are common culprits. These targeted changes should reveal whether the problem is the blocking cin, an out‑of‑bounds write, or wrong runtime types.

Do you really expect us to analyze and comment on almost 1000 lines of code? I'd be happy to for my normal consulting rate of $200 USD per hour... :-(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.