Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 204
  • Last Modified:

How to implement an array

#include <iostream>
#include <iomanip>
#include <math.h>
#include <string>

using namespace std;

int main()
{
       char            Answer;
     
//*****Loop*****
do{
       //****PROGRAM TITLE*****
     cout << "\t\t\t*************"<<endl;
       cout << "\t\t\tSTOCK PROGRAM      "<<endl;
       cout << "\t\t\t*************\n\n"<<endl;
      
     //****DEFINE VARIABLES****
     int        Shares = 0; //# OF STOCK SHARES OWNED
     double     MarketPrice = 0.00;//PRICE PER SHARE OF STOCK
     double     CommissionFee = 0.02;//COMMISSION FEE PER STOCK
     double     CommissionMinimumFee = 39.95; //MINIMUM COMMISSION FEE
       double     MarketValue; //TOTAL VALUE
       double     CommissionPaid; //COST OF COMMISSION
       double            TotalAmountPaidCF;
       double            TotalAmountPaidCMF;
       int            UserMenu;
       string            StockMarket;
       double            NYSE = 0.00;
       double            AMEX = 0.00;
       double            OTC = 0.00;
       double            NASDAQ = 0.00;       
      
       //****SET 2 DECIMAL PLACES*****
       cout << setprecision(2);
       cout.setf(ios::fixed);
       cout.setf(ios::right);

       //****USER MENU*****
       cout << "\nNYSE\t = 0" << endl;
       cout << "\nAMEX\t = 1" << endl;
       cout << "\nOTC\t = 2" << endl;
       cout << "\nNASDAQ\t = 3" << endl;
      
       cout << "\nTo QUIT\t = 4" <<endl;
            
       cout << "\nPlease Choose Stock Market:  ";
             
       //****USER INPUT FOR STOCK MARKET******
       cin >> UserMenu;

             
     //****USER INPUT FOR SHARES*****
       while (Shares <= 0){
             cout << endl;
             cout<< "Please enter number of shares: ";
             cin >> Shares;
     
       //****ERROR CHECK*****
             if (Shares <= 0)
                   cout << endl << "You entered invalid information, Please try again!" <<endl;
       }

       //****USER INPUT FOR MARKET PRICE*****
       while (MarketPrice <= 0.00){
             cout << endl;
             cout<< "Please enter Market Price Per Share: $";
             cin >> MarketPrice;
     
       //****ERROR CHECK*****
             if (MarketPrice <= 0)
                   cout << endl << "You entered invalid information, Please try again!" <<endl;
       }
      
      
       //****SWITCH*****
       switch (UserMenu){
             case 0:
                   StockMarket = "NYSE";
                   CommissionFee = 0.02;
                   break;
             case 1:
                   StockMarket = "AMEX";
                   CommissionFee = 0.025;
                   break;
             case 2:
                   StockMarket = "OTC";
                   CommissionFee = 0.03;
                   break;
             case 3:
                   StockMarket = "NASDAQ";
                   CommissionFee = 0.05;
                   break;
             case 4:
                   return 0;

       }
     //****CALCULATION*****
     MarketValue = MarketPrice * Shares;//CALCULATION TO DETERMINE VALUE OF STOCKS
     CommissionPaid      = CommissionFee * Shares;//CALCULATION TO DETERMINE COMMISSION PER STOCK
       TotalAmountPaidCMF = CommissionMinimumFee + MarketValue;
       TotalAmountPaidCF = MarketValue + CommissionPaid;
      
       //****DISPLAY*****
       cout << "\n\nStock Market:\t\t\t" << StockMarket << endl;
     cout << "\n\nShares:\t\t\t\t " << Shares << endl;
     cout << "\nMarket Price Per Share:\t\t $" << MarketPrice << endl;
     cout << "\nMarket Value:\t\t\t $" << MarketValue << endl;
      
       //****CONDITIONAL STATEMENT FOR COMMISSION*****
     if (CommissionPaid < CommissionMinimumFee) {
            cout << "\nCommission Paid is :\t\t $" << CommissionMinimumFee << endl;
            cout << "\n\n\nTotal Amount Paid:\t\t  $" << TotalAmountPaidCMF << endl;
     }

     else if (CommissionPaid > CommissionMinimumFee) {
            cout << "\nCommission Paid is :\t\t $" << CommissionPaid << endl;    
            cout << "\n\n\nTotal Amount Paid:\t\t  $" << TotalAmountPaidCF << endl;
     }

       //****DISPLAY USER OPTIONS TO END OR CONTINUE PROGRAM*****
       cout << "\n\nDo you want to continue enter (Y/N) then press enter?\n";
       cout<< "\nYou must type a 'Y' or an 'N'.";
     cin >> Answer;

//****END LOOP*****
}
      //****CONDITIONAL STATMENT FOR LOOP*****
      while ((Answer == 'Y') || (Answer == 'y'));
            if (Answer = 'N')
                  return 0;
            else if (Answer = 'n')
                  return 0;         
cin.ignore();
cin.ignore();

//*****END PROGRAM*****
}
0
mmorse0971
Asked:
mmorse0971
1 Solution
 
Jaime OlivaresCommented:
And the specific question is........
0
 
mmorse0971Author Commented:
Sorry getting late.  I am trying to implement an array in this program but not sure if I can and/or on to incorporate it into this program
0
 
AxterCommented:
You have a better chance of getting a complete answer, if you post a detailed question.

Please give us more information as to exactly what you're looking for.

The more information the better.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
novitiateCommented:
you can do some thing like this.
#include <iostream>
#include <iomanip>
#include <math.h>
#include <string>
using namespace std;
class Share
{
public:
      Share(int code, float value = 0):_nCode(code), _fPrice(value)
      {
      }
      float getValue() const {return _fPrice;}
      float setValue(float value){ _fPrice = value;}

      float _fPrice;

private:
      int _nCode;
};

class StockMarket
{
public:
      StockMarket(string name, float CommissionFee, float MinCommission = 39.95)
      {
            _szName = name;
            _fCommissionFee = CommissionFee;
            _fCommissionMinFee = MinCommission;
      }
      float getMarketValue(const Share & s, int nShares) const
      {
            return s.getValue() * nShares;//CALCULATION TO DETERMINE VALUE OF STOCKS
      
      }
      float getCommissionPaid(int nShares) const
      {
            return _fCommissionFee * nShares;//CALCULATION TO DETERMINE COMMISSION PER STOCK
      }
      
      float getTotalAmountPaidCMF(const Share & s, int nShares) const
      {
            return _fCommissionMinFee + getMarketValue(s, nShares);
      }
      
      float getTotalAmountPaidCF(const Share & s, int nShares) const
      {
            return getMarketValue(s, nShares) + getCommissionPaid(nShares);
      }

      const string & getName() const
      {
            return _szName;
      }

      float getCommissionFee()
      {
            return _fCommissionFee;
      }
      float getCommissionMinFee()
      {
            return _fCommissionMinFee;
      }
private:
      string _szName;
      float _fCommissionFee;
      float _fCommissionMinFee;
};

using namespace std;

int main()
{
      char          Answer;
      
      //*****Loop*****
      do{
            //****PROGRAM TITLE*****
            cout << "\t\t\t*************"<<endl;
            cout << "\t\t\tSTOCK PROGRAM     "<<endl;
            cout << "\t\t\t*************\n\n"<<endl;
            
            //****DEFINE VARIABLES****
            int        Shares = 0; //# OF STOCK SHARES OWNED
            Share         share(1, 0.00);
            
      
            int          UserMenu;
            double          NYSE = 0.00;
            double          AMEX = 0.00;
            double          OTC = 0.00;
            double          NASDAQ = 0.00;      
            
            //****SET 2 DECIMAL PLACES*****
            cout << setprecision(2);
            cout.setf(ios::fixed);
            cout.setf(ios::right);
            
            //****USER MENU*****
            cout << "\nNYSE\t = 0" << endl;
            cout << "\nAMEX\t = 1" << endl;
            cout << "\nOTC\t = 2" << endl;
            cout << "\nNASDAQ\t = 3" << endl;
            
            cout << "\nTo QUIT\t = 4" <<endl;
            
            cout << "\nPlease Choose Stock Market:  ";
            
            //****USER INPUT FOR STOCK MARKET******
            cin >> UserMenu;
            
            
            //****USER INPUT FOR SHARES*****
            while (Shares <= 0){
                  cout << endl;
                  cout<< "Please enter number of shares: ";
                  cin >> Shares;
                  
                  //****ERROR CHECK*****
                  if (Shares <= 0)
                cout << endl << "You entered invalid information, Please try again!" <<endl;
            }
            
            //****USER INPUT FOR MARKET PRICE*****
            while (share._fPrice <= 0.00){
                  cout << endl;
                  cout<< "Please enter Market Price Per Share: $";
                  cin >> share._fPrice;
                  
                  //****ERROR CHECK*****
                  if (share._fPrice <= 0)
                cout << endl << "You entered invalid information, Please try again!" <<endl;
            }
            
            StockMarket *pStockMarket = NULL;
            
            //****SWITCH*****
            switch (UserMenu){
            case 0:
                  pStockMarket = new StockMarket("NYSE", 0.02);
                  
                  break;
            case 1:
                  pStockMarket = new StockMarket("AMEX", 0.025);
                  
                  break;
            case 2:
                  pStockMarket = new StockMarket("OTC", 0.03);
                  
                  break;
            case 3:
                  pStockMarket = new StockMarket("NASDAQ", 0.05);
                  
                  break;
            case 4:
                  return 0;
                  
            }
            
            //****DISPLAY*****
            cout << "\n\nStock Market:\t\t\t" << pStockMarket->getName() << endl;
            cout << "\n\nShares:\t\t\t\t " << Shares << endl;
            cout << "\nMarket Price Per Share:\t\t $" << share._fPrice << endl;
            cout << "\nMarket Value:\t\t\t $" << pStockMarket->getMarketValue(share, Shares) << endl;
            
            //****CONDITIONAL STATEMENT FOR COMMISSION*****
            if (pStockMarket->getCommissionPaid(Shares) < pStockMarket->getCommissionMinFee()) {
                  cout << "\nCommission Paid is :\t\t $" << pStockMarket->getCommissionMinFee() << endl;
                  cout << "\n\n\nTotal Amount Paid:\t\t  $" << pStockMarket->getTotalAmountPaidCMF(share, Shares) << endl;
            }
            
            else {
                  cout << "\nCommission Paid is :\t\t $" << pStockMarket->getCommissionPaid(Shares) << endl;    
                  cout << "\n\n\nTotal Amount Paid:\t\t  $" << pStockMarket->getTotalAmountPaidCF(share, Shares) << endl;
            }
            
            //****DISPLAY USER OPTIONS TO END OR CONTINUE PROGRAM*****
            cout << "\n\nDo you want to continue enter (Y/N) then press enter?\n";
            cout<< "\nYou must type a 'Y' or an 'N'.";
            cin >> Answer;
            
            //****END LOOP*****
      }
      //****CONDITIONAL STATMENT FOR LOOP*****
      while ((Answer == 'Y') || (Answer == 'y'));
      if (Answer = 'N')
            return 0;
      else if (Answer = 'n')
            return 0;        
      cin.ignore();
      cin.ignore();
      
      //*****END PROGRAM*****
      return 0;
}

_novi_
0
 
novitiateCommented:
where do you want to use the array?

If you have multiple stocks/shares, then you can use any of STL collections, like vector.

by the way I missed 'delete pStockMarket;' there.

_novi_
0
 
smpoojaryCommented:
I think you want to store MarketValue, CommissionPaid, TotalAmountPaidCMF and TotalAmountpaidCF to array. For handle n number of user stock information...

   //Declare as for handle 100 records of information
      double     MarketValue[100]; //TOTAL VALUE
      double     CommissionPaid[100]; //COST OF COMMISSION
      double          TotalAmountPaidCF[100];
      double          TotalAmountPaidCMF[100];

     ...
int i=0;
do
{
   ...
   //****CALCULATION*****
     MarketValue[i] = MarketPrice * Shares;//CALCULATION TO DETERMINE VALUE OF STOCKS
     CommissionPaid[i]     = CommissionFee * Shares;//CALCULATION TO DETERMINE COMMISSION PER STOCK
      TotalAmountPaidCMF[i] = CommissionMinimumFee + MarketValue;
      TotalAmountPaidCF[i] = MarketValue + CommissionPaid;
...
    ++i;
}while ((Answer == 'Y') || (Answer == 'y'));
n = i; // n number of records
...
//Do other calculation here using array of stock information
...

YOu can also take input values from file. What you say?
-Mahesh

0
 
itsmeandnobodyelseCommented:
>>>> I am trying to implement an array in this program

I assume you want to store user input into an array ...

If yes, you could try that:

#include <iostream>
#include <iomanip>
#include <math.h>
#include <string>
#include <vector>
using namespace std;

enum Market {  INITIAL = -1, NYSE, AMEX, OTC, NASDAQ, NUM_MARKETS };

struct Share
{
    Market    market;
    int          shares;
    double     price;
   
    Share() : market(INITIAL), shares(0), price(0.0) {}
    Share(Market m, int s, double p) : market(m), shares(s), price(p) {}
};    

const char*  StockMarkets[NUM_MARKETS]   = { "NYSE", "AMEX", "OTC", "NASDAQ" };
double       CommissionFees[NUM_MARKETS] = { 0.02, 0.025, 0.03, 0.05 };

int main()
{
    char                 Answer;
    vector<Share>  ShareArr;
    double              MarketValues[NUM_MARKETS] = { 0.00 }; // not used
     
    //*****Loop*****
    do
    {
        //****PROGRAM TITLE*****
        cout << "\t\t\t*************"<<endl;
        cout << "\t\t\tSTOCK PROGRAM     "<<endl;
        cout << "\t\t\t*************\n\n"<<endl;
       
        //****DEFINE VARIABLES****
        int        Shares = 0; //# OF STOCK SHARES OWNED
        double     MarketPrice = 0.00;//PRICE PER SHARE OF STOCK
        double     CommissionFee = 0.02;//COMMISSION FEE PER STOCK
        double     CommissionMinimumFee = 39.95; //MINIMUM COMMISSION FEE
        double     MarketValue; //TOTAL VALUE
        double     CommissionPaid; //COST OF COMMISSION
        double     TotalAmountPaidCF;
        double     TotalAmountPaidCMF;
        int        UserMenu;
       
        string     StockMarket;
        Market     MarketID;
       
        //****SET 2 DECIMAL PLACES*****
        cout << setprecision(2);
        cout.setf(ios::fixed);
        cout.setf(ios::right);
       
        //****USER MENU*****
        UserMenu = -1;
        while (UserMenu < 0 || UserMenu > 4)
        {
            cout << "\nNYSE\t = 0" << endl;
            cout << "\nAMEX\t = 1" << endl;
            cout << "\nOTC\t = 2" << endl;
            cout << "\nNASDAQ\t = 3" << endl;
           
            cout << "\nTo QUIT\t = 4" <<endl;
           
            cout << "\nPlease Choose Stock Market:  ";
           
            //****USER INPUT FOR STOCK MARKET******
            cin >> UserMenu;
            //****ERROR CHECK*****
            if (UserMenu < 0 || UserMenu > 4)
                cout << endl << "You entered invalid information, Please try again!" <<endl;
        }
        if (UserMenu == 4)
            return 0;
       
        //****USER INPUT FOR SHARES*****
        while (Shares <= 0)
        {
            cout << endl;
            cout<< "Please enter number of shares: ";
            cin >> Shares;
           
            //****ERROR CHECK*****
            if (Shares <= 0)
                cout << endl << "You entered invalid information, Please try again!" <<endl;
        }
       
        //****USER INPUT FOR MARKET PRICE*****
        while (MarketPrice <= 0.00)
        {
            cout << endl;
            cout<< "Please enter Market Price Per Share: $";
            cin >> MarketPrice;
           
            //****ERROR CHECK*****
            if (MarketPrice <= 0)
                cout << endl << "You entered invalid information, Please try again!" <<endl;
        }
       
        MarketID      = (Market)UserMenu;    // cast user input to enum
        StockMarket   = StockMarkets[MarketID];
        CommissionFee = CommissionFees[MarketID];      
       
        //****ADD INPUT TO ARRAY*****
        ShareArr.push_back(Share(MarketID, Shares, MarketPrice));
       
        //****CALCULATION*****
        MarketValue = MarketPrice * Shares;//CALCULATION TO DETERMINE VALUE OF STOCKS
        CommissionPaid     = CommissionFee * Shares;//CALCULATION TO DETERMINE COMMISSION PER STOCK
        TotalAmountPaidCMF = CommissionMinimumFee + MarketValue;
        TotalAmountPaidCF = MarketValue + CommissionPaid;
       
        //****DISPLAY*****
        cout << "\n\nStock Market:\t\t\t" << StockMarket << endl;
        cout << "\n\nShares:\t\t\t\t " << Shares << endl;
        cout << "\nMarket Price Per Share:\t\t $" << MarketPrice << endl;
        cout << "\nMarket Value:\t\t\t $" << MarketValue << endl;
       
        //****CONDITIONAL STATEMENT FOR COMMISSION*****
        if (CommissionPaid < CommissionMinimumFee)
        {
            cout << "\nCommission Paid is :\t\t $" << CommissionMinimumFee << endl;
            cout << "\n\n\nTotal Amount Paid:\t\t  $" << TotalAmountPaidCMF << endl;
        }
       
        else if (CommissionPaid > CommissionMinimumFee)
        {
            cout << "\nCommission Paid is :\t\t $" << CommissionPaid << endl;    
            cout << "\n\n\nTotal Amount Paid:\t\t  $" << TotalAmountPaidCF << endl;
        }
       
        //****DISPLAY USER OPTIONS TO END OR CONTINUE PROGRAM*****
        cout << "\n\nDo you want to continue enter (Y/N) then press enter?\n";
        cout<< "\nYou must type a 'Y' or an 'N'.";
        cin >> Answer;
       
        //****END LOOP*****
       
    }
    while ((Answer == 'Y') || (Answer == 'y'));  //****CONDITIONAL STATMENT FOR LOOP*****

    //***** NEED TO EVALUATE THE ARRAY HERE*****

    return 0;
    //*****END PROGRAM*****
}

Regards, Alex

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now