mmorse0971
asked on
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*************"<<end l;
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*****
}
#include <iomanip>
#include <math.h>
#include <string>
using namespace std;
int main()
{
char Answer;
//*****Loop*****
do{
//****PROGRAM TITLE*****
cout << "\t\t\t*************"<<end
cout << "\t\t\tSTOCK PROGRAM "<<endl;
cout << "\t\t\t*************\n\n"<
//****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*****
}
And the specific question is........
ASKER
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
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.
Please give us more information as to exactly what you're looking for.
The more information the better.
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(cons t 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*************"<<end l;
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->getMarketVal ue(share, Shares) << endl;
//****CONDITIONAL STATEMENT FOR COMMISSION*****
if (pStockMarket->getCommissi onPaid(Sha res) < pStockMarket->getCommissio nMinFee()) {
cout << "\nCommission Paid is :\t\t $" << pStockMarket->getCommissio nMinFee() << endl;
cout << "\n\n\nTotal Amount Paid:\t\t $" << pStockMarket->getTotalAmou ntPaidCMF( share, Shares) << endl;
}
else {
cout << "\nCommission Paid is :\t\t $" << pStockMarket->getCommissio nPaid(Shar es) << endl;
cout << "\n\n\nTotal Amount Paid:\t\t $" << pStockMarket->getTotalAmou ntPaidCF(s hare, 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_
#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(cons
{
return _fCommissionMinFee + getMarketValue(s, nShares);
}
float getTotalAmountPaidCF(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*************"<<end
cout << "\t\t\tSTOCK PROGRAM "<<endl;
cout << "\t\t\t*************\n\n"<
//****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->getMarketVal
//****CONDITIONAL STATEMENT FOR COMMISSION*****
if (pStockMarket->getCommissi
cout << "\nCommission Paid is :\t\t $" << pStockMarket->getCommissio
cout << "\n\n\nTotal Amount Paid:\t\t $" << pStockMarket->getTotalAmou
}
else {
cout << "\nCommission Paid is :\t\t $" << pStockMarket->getCommissio
cout << "\n\n\nTotal Amount Paid:\t\t $" << pStockMarket->getTotalAmou
}
//****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_
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_
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_
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
//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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.