- #1
twotaileddemon
- 260
- 0
My final exam is tuesday and I am having an extremely extremely hard time grasping arrays and strings. I just can't visualize how they work and I am stuck on how to get my printOwnedShares function to work properly in a project that will help me study for the final. This is what I have worked on (its taking me over 30 hours and I've been trying really hard):
and this is a sample run:
What I want is for the "you currenly own:" part to print the correct shares corresponding to the correct name of the company. I think I need an if/else statement instead of the for statement or I am not using it correctly in the first place. I want more than anything to be able to understand this
Code:
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
// PROTOTYPE FUNCTIONS
void printMenu(string name[], double price[], int size);
int getCompany(string name[], double price[], int size);
double getShares(string name);
double calculateCost(char stock, int shares);
double calculateCommission(double shares, double cost);
void printPurchase(double shares, double cost,
double commission, string name);
bool anotherTransaction();
void printSummary(int purchases, double total_cost,
double total_commission);
// Project 8
double getPrice (string name);
void getMenu (string name[], double price[], int size);
void printOwnedShares (string name[], double shares[], int size);
int main ( )
{
// SET PRECISION
cout.setf(ios::fixed, ios::floatfield);
cout.precision(2);
cout.setf(ios::showpoint);
/* **********************************
Declare Symbolic Constants
********************************** */
const int MAX = 8;
/* **********************************
Declare Variables
********************************** */
int stock;
bool transaction;
double cost, commission;
/* ********* PROJECT 8 */
int index;
double price[MAX];
double shares[MAX];
string name[MAX];
int current_size = 1;
int purchases = 0;
double total_cost = 0.0;
double total_commission = 0.0;
/* **********************************
Input
********************************** */
cout << "Welcome to FT's Stockbroker Program." << endl;
// PROJECT 8
getMenu(name, price, MAX);
// ANOTHER TRANSACTION
transaction = anotherTransaction();
while(transaction == true)
{
// NUMBER OF PURCHASES
purchases = purchases + 1;
// RETURN CHOICE OF COMPANY
stock = getCompany(name, price, MAX);
// RETURN NUMBER OF SHARES
if (stock == 1)
shares[index] = getShares(name[0]);
else if (stock == 2)
shares[index] = getShares(name[1]);
else if (stock == 3)
shares[index] = getShares(name[2]);
else if (stock == 4)
shares[index] = getShares(name[3]);
else if (stock == 5)
shares[index] = getShares(name[4]);
else if (stock == 6)
shares[index] = getShares(name[5]);
else if (stock == 7)
shares[index] = getShares(name[6]);
else
shares[index] = getShares(name[7]);
// PRINT CURRENTLY OWNED SHARES
printOwnedShares(name, shares, current_size);
current_size = current_size + 1;
/* **********************************
Compute
********************************** */
// CALCULATE COST OF PURCHASE
cost = shares[index] * price[index];
total_cost = total_cost + cost;
// CALCULATE COMMISSION
commission = calculateCommission(shares[index], cost);
total_commission = total_commission + commission;
/* **********************************
Output
********************************** */
// PRINT DETAILS OF PURCHASE
printPurchase(shares[index], cost, commission, name[index]);
// UPDATE FOR TRANSACTION WHILE LOOP
transaction = anotherTransaction();
}
// TOTAL PURCHASES, COST, AND COMMISSION
printSummary(purchases, total_cost, total_commission);
cout << "Thank you for using (my name)'s Stockbroker." << endl;
// Return that program executed successfully
return EXIT_SUCCESS;
}
void printMenu(string name[], double price[], int size)
{
int index;
cout << "Here is a menu of stocks you can purchase:" << endl;
for (index = 0; index < size; index++)
{
cout << "Enter " << index + 1 << " for " << name[index] << endl;
}
return;
}
int getCompany(string name[], double price[], int size)
{
int stock;
do
{
// PRINT MENU OF STOCKS
printMenu(name, price, size);
cin >> stock;
} while (stock < 1 || stock > 8);
return stock;
}
double getShares(string name)
{
double shares;
int index;
do
{
cout << "How many shares of " << name
<< " would you like to buy? " << endl;
cin >> shares;
}while (shares < 0);
return shares;
}
double calculateCommission(double shares, double cost)
{
const double HUNDRED_OR_LESS_COMMISSION = 9.99;
const double THOUSAND_OR_LESS_COMMISSION = 19.95;
const double THOUSAND_OR_MORE_COMMISSION = .025;
double commission;
int index;
if(shares < 100)
{
commission = HUNDRED_OR_LESS_COMMISSION;
}
else
{
if(shares >= 1000)
{
commission = THOUSAND_OR_MORE_COMMISSION * cost;
}
else
{
commission = THOUSAND_OR_LESS_COMMISSION;
}
}
return commission;
}
void printPurchase(double shares, double cost,
double commission, string name)
{
int index;
cout << "The cost of " << shares << " shares of "
<< name << " is $" << cost << "." << endl;
cout << "The commission paid is $" << commission << endl;
return;
}
bool anotherTransaction()
{
char transaction;
do
{
cout << "Would you like to buy another stock (y/Y/n/N)?" << endl;
cin >> transaction;
transaction = tolower(transaction);
}while (!(transaction == 'y' || transaction == 'n'));
if(transaction == 'y')
{
transaction = true;
}
else
{
transaction = false;
}
return transaction;
}
void printSummary(int purchases, double total_cost,
double total_commission)
{
cout << "You have made " << purchases << " purchases." << endl
<< "The total cost is $" << total_cost << "." << endl
<< "The total commission is $" << total_commission << "."
<< endl;
return;
}
// PROJECT 8
double getPrice (string name)
{
double price;
int index;
do
{
cout << "Enter the price of a " << name
<< " share." << endl;
cin >> price;
} while (price < 0);
return price;
}
void getMenu(string name[], double price[], int size)
{
int index;
for (index = 0; index < size; index++)
{
cout << "Enter the name of company " << index + 1 << endl;
cin >> name[index];
price[index] = getPrice (name[index]);
}
return;
}
void printOwnedShares(string name[], double shares[], int size)
{
int index;
cout << "You currently own: " << endl;
for (index = 0; index < size; index++)
{
cout << shares[index] << " shares of " << name[index] << endl;
}
return;
}
and this is a sample run:
Enter the name of company 1
a
Enter the name of company 2
b
Enter the name of company 3
c
Enter the name of company 4
d
Enter the name of company 5
e
Enter the name of company 6
f
Enter the name of company 7
g
Enter the name of company 9
h
Would you like to buy another stock (y/Y/n/N)?
y
Here is a menu of stocks you can purchase:
Enter 1 for a
Enter 2 for b
Enter 3 for c
Enter 4 for d
Enter 5 for e
Enter 6 for f
Enter 7 for g
Enter 8 for h
3
How many shares of c would you like to buy?
100
You currently own:
100.00 shares of a
The cost of 100.00 shares of a is $1000.00.
The commission paid is $19.95
Would you like to buy another stock (y/Y/n/N)?
y
Here is a menu of stocks you can purchase:
Enter 1 for a
Enter 2 for b
Enter 3 for c
Enter 4 for d
Enter 5 for e
Enter 6 for f
Enter 7 for g
Enter 8 for h
7
How many shares of g would you like to buy?
200
You currently own:
200.00 shares of a
0.00 shares of b
The cost of 200.00 shares of a is $2000.00.
The commission paid is $19.95.
Would you like to buy another stock (y/Y/n/N)?
n
You have made 2 purchases.
The total cost is $3000.00.
The total commission is $39.90.
Thank you for using (my name)'s Stockbroker.
What I want is for the "you currenly own:" part to print the correct shares corresponding to the correct name of the company. I think I need an if/else statement instead of the for statement or I am not using it correctly in the first place. I want more than anything to be able to understand this
Last edited by a moderator: