Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The ABC brokerage company asks you to write a stock account program. Each user w

ID: 3544240 • Letter: T

Question

The ABC brokerage company asks you to write a stock account program. Each user will start with an account balance of $1000. Your program should let the user                    choose the number of shares from a list of 10 stocks. Your program will read in the stock data from a file named stocks.dat. This file includes the stock                    symbol, company name, and current price for the 10 stocks. After the user has purchased his or her stocks by entering the number of shares and the stock                    symbol, your program should simulate a day on the stock market. During the course of the day, the price of each stock will rise or fall by zero to 99 percent.                    At the end of the market day, your program should recalculate the value of the user

Explanation / Answer

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring>
#include <cstdlib>
#include <ctime>

const int SYMB_LEN = 8;
const int NAME_LEN = 24;
const int STOCK_LEN = 10;

void read_stocks(char[][SYMB_LEN], char[][NAME_LEN], double[]);
void select_stocks_buy(char[][SYMB_LEN], char[][NAME_LEN], double[],
                       double&, int[]);
void select_stocks_sell(char[][SYMB_LEN], char[][NAME_LEN], double[],
                        double&, int[]);
void simulate_day(char[][SYMB_LEN], char[][NAME_LEN], double[]);
void daily_recap(char[][SYMB_LEN], char[][NAME_LEN], double[],
                 double &, int[]);
void final_recap(char[][SYMB_LEN], char[][NAME_LEN], double[],
                 double &, int[]);
double calc_port(double[], double &, int[]);

int main()
{
    srand(time(0));
    char symbols[STOCK_LEN][SYMB_LEN];
    char names[STOCK_LEN][NAME_LEN];
    double values[STOCK_LEN];

    double balance = 1000.0;
    int amounts[STOCK_LEN] = {};

    char decision = 'b';

    read_stocks(symbols, names, values);

    for (int day = 1; day <= 5; ++day)
    {
        std::cout << "***************************************************** "
                  << "*** Welcome to day " << day
                  << " of virtual stock simulation! *** "
                  << "***************************************************** ";
        if (toupper(decision) == 'B')
            select_stocks_buy(symbols, names, values, balance, amounts);
        else if (decision == 'S' || decision == 's')
            select_stocks_sell(symbols, names, values, balance, amounts);

        simulate_day(symbols, names, values);

        if (day < 5)
        {
            daily_recap(symbols, names, values, balance, amounts);
            do {
                std::cout << "Do you want to (B)uy or (S)ell next? ";
                std::cin >> decision;
            } while (toupper(decision) == 'B' && toupper(decision) == 'S');
        }
        std::cout << " ";
    }
    final_recap(symbols, names, values, balance, amounts);
}


void simulate_day(char symbols[][SYMB_LEN], char names[][NAME_LEN],
                  double values[])
{
    std::cout << " Daily change: ";
    for (int i = 0; i < STOCK_LEN; ++i)
    {
        double change_pct = (rand() % 199 - 99) / 100.0;
        double change_val = values[i] * change_pct;
        values[i] += change_val;
        std::cout.precision(2);
        std::cout << std::left << std::setw(SYMB_LEN) << symbols[i]
                  << std::right << std::fixed << std::setw(10) << values[i]
                  << " (" << (change_val > 0 ? "+" : "")
                  << std::fixed << change_val << ") ";
    }
}

void final_recap(char symbols[][SYMB_LEN], char names[][NAME_LEN],
                 double values[], double& balance, int amounts[])
{
    std::ofstream fout("account.dat");
    fout.precision(2);
    std::cout.precision(2);
    std::cout << " Your account's balance: $" << std::fixed << balance << " "
              << " Your current stock holdings: ";
    fout << "Your account's balance: $" << std::fixed << balance << " "
         << " Your current stock holdings: ";
    for (int i = 0; i < STOCK_LEN; ++i) if (amounts[i])
    {
        std::cout << std::left << std::setw(SYMB_LEN) << symbols[i]
                  << std::setw(NAME_LEN) << names[i]
                  << std::right << std::setw(8) << values[i]
                  << std::setw(6) << amounts[i] << " ";
        fout << std::left << std::setw(SYMB_LEN) << symbols[i]
             << std::setw(NAME_LEN) << names[i]
             << std::right << std::setw(8) << values[i]
             << std::setw(6) << amounts[i] << " ";
    }
    std::cout << " Your account's worth: $"
              << std::fixed << calc_port(values, balance, amounts) << " ";
    fout << " Your account's worth: $"
         << std::fixed << calc_port(values, balance, amounts) << " ";
    fout.close();
}

void daily_recap(char symbols[][SYMB_LEN], char names[][NAME_LEN],
                 double values[], double& balance, int amounts[])
{
    std::cout.precision(2);
    std::cout << " Your account's balance: $" << std::fixed << balance << " "
              << "Your account's worth: $"
              << std::fixed << calc_port(values, balance, amounts) << " ";
}

double calc_port(double values[], double& balance, int amounts[])
{
    double share_value = 0.0;
    for (int i = 0; i < STOCK_LEN; ++i)
        share_value += amounts[i] * values[i];
    return share_value + balance;
}

int get_symbol_index(char symbols[][SYMB_LEN], char symbol[SYMB_LEN])
{
    for (int i = 0; i < STOCK_LEN; ++i)
        if (strcmp(symbols[i], symbol) == 0)
            return i;
    return -1;
}

void select_stocks_buy(char symbols[][SYMB_LEN], char names[][NAME_LEN],
                       double values[], double& balance, int amounts[])
{
    char stock_symbol[SYMB_LEN];

    std::cout << std::fixed << std::setprecision(2);
    for (int i = 0; i < STOCK_LEN; ++i)
    {
        std::cout << std::left << std::setw(SYMB_LEN) << symbols[i]
                  << std::setw(NAME_LEN) << names[i]
                  << std::right << std::setw(8) << values[i] << " ";
    }

    std::cout << "Enter stock symbol to buy: ";
    std::cin >> stock_symbol;
    int shares;
    std::cout << "Number of shares: ";
    std::cin >> shares;

    int buy_index = get_symbol_index(symbols, stock_symbol);
    if (buy_index == -1)
    {
        std::cout << "Cannot find stock symbol ";
        return;
    }
    double spent = shares * values[buy_index];
    if (spent > balance)
    {
        std::cout << "You can't buy all that! ";
        return;
    }
    balance -= spent;
    amounts[buy_index] += shares;
    std::cout << "You bought " << shares << " " << names[buy_index]
              << "'s share(s) ";
}

void select_stocks_sell(char symbols[][SYMB_LEN], char names[][NAME_LEN],
                        double values[], double& balance, int amounts[])
{
    char stock_symbol[SYMB_LEN];

    std::cout << std::fixed << std::setprecision(2);
    for (int i = 0; i < STOCK_LEN; ++i) if (amounts[i])
    {
        std::cout << std::left << std::setw(SYMB_LEN) << symbols[i]
                  << std::setw(NAME_LEN) << names[i]
                  << std::right << std::setw(8) << values[i]
                  << std::setw(6) << amounts[i] << " ";
    }

    std::cout << "Enter stock symbol to sell: ";
    std::cin >> stock_symbol;
    int shares;
    std::cout << "Number of shares: ";
    std::cin >> shares;

    int sell_index = get_symbol_index(symbols, stock_symbol);
    if (sell_index == -1)
    {
        std::cout << "Cannot find stock symbol ";
        return;
    }
    if (shares > amounts[sell_index])
    {
        std::cout << "You can't sell that much! ";
        return;
    }
    double gained = shares * values[sell_index];
    balance += gained;
    amounts[sell_index] -= shares;
    std::cout << "You sold " << shares << " " << names[sell_index]
              << " shares(s) ";
}

void read_stocks(char symb[][SYMB_LEN], char name[][NAME_LEN], double val[])
{
    std::ifstream fin("stocks.dat");
    if (!fin) { std::cerr << "Cannot open stocks file "; exit(1); }
    for (int i = 0; i < STOCK_LEN; ++i)
    {
        fin >> symb[i];
        fin.ignore(100, ' ');
        fin.getline(name[i], NAME_LEN);
        fin >> val[i];
    }
    fin.close();
}



with indentation: http://codepad.org/czXXFlwS