c++ PROBLEM: Create a menu driven application to keep track of your stock portfo
ID: 3743943 • Letter: C
Question
c++
PROBLEM: Create a menu driven application to keep track of your stock portfolio. Create a structure called StockInfo that holds: Company name Number of shares Purchase price Current price The current value of a stock is calculated as: Number of shares * (current price – purchase price) Create an array in main that can hold a maximum of 10 StockInfo objects. Your program will have the following menu options: 1-Add Stock 2-Display Profit/Loss 3 Exit Program For option 1 Prompt the user for info to add one new stock to the array: Enter company name: Amgen Number of shares? 200 Purchase price? 50.75 Current price? 75.625 Make sure there is room in the array… if it is full, give a message to the user. Otherwise, store in the next available position. For option 2 Display a report based on all the stocks in the array as shown below: Portfolio Report ================ Company Profit(Loss) Amgen $ 4975.00 Delta $ -1450.00 Total value $ 3525.00 Validation of number of shares and current/purchase price is optional. I will only test with valid data. Your program should make sure the menu option is valid (give a message if not), and that you do not overflow your array. ***USE FUNCTIONS to organize your code! Main should ONLY have the loop to control the menu and calls to functions for each option: For option1: two possibilities, choose one addRecord needs the array and the next available position to add a new stock. Prompts for the data and adds the element to the array or createStock takes no input. Prompts for the data and returns a Stock object For option 2: displayReport needs the array and the number of ACTUAL items used in the array You should code and test one function at a time! Testing – show one run with THIS DATA, and another with data OF YOUR OWN use option 1 to add a stock Company shares purchase current Amgen 200 50.75 75.625 use option 1 to add a stock Delta 100 111.75 97.25 Option 2 to show report Option 1 three more times to add these stocks Company shares Purchase Current Cisco 400 22.00 16.50 Intuit 250 38.00 44.50 ToysRUs 300 15.90 17.95 Option 2 for report Quit (You should make sure the results are correct!) it said to do functions for this question, i cant seem to figure out how to do functions here #include #include #include using namespace std; struct StockInfo { string coname; float numShares; float PurPrice; float CurrPrice; }; void addRecord (struct StockInfo, int size); void displayResult ( struct StockInfo, int size); int main() { const int size =2; StockInfo portfolio[size]; int count=0; int choice; do { cout << endl << " 1 - Add a stock. " << " 2 - Display Profit/loss. " << " 3 - Exit Program. " << " Enter your choice and press return: "; cin >> choice; switch (choice) { case 1: void addRecord (struct StockInfo,int size); break; case 2: void displayResult (struct StockInfo,int size); //Number of shares * (current price – purchase price) break; case 3: //Exit the program break; default: cout << "Not a Valid Choice. "<< "Choose again. "; break; } } while(choice <3); return 0; } void addRecord (struct StockInfo portfolio,int size) { cout << "enter company's name please:"<>portfolio[i].coname; cout << "enter the number of shares bought:"<> portfolio[i].numShares; cout <<"What was the purchase price?"<> portfolio[i].PurPrice; cout << "what is the current price of the share?"<> portfolio[i].CurrPrice; cin.ignore(); count++ } void displayResult (struct StockInfo portfolio, int size) { cout<<"Portfolio Report ================"<
Explanation / Answer
#include <iostream>
#include <string>
#include <iomanip>
#include <stdlib.h>
using namespace std;
// Defines a structure StockInfo
struct StockInfo
{
// Data member to store stock information
string coname;
float numShares;
float PurPrice;
float CurrPrice;
};// End of structure
// Function to accept a stock information and returns the StockInfo object
struct StockInfo addRecord ()
{
// Declares a temporary object
struct StockInfo temp;
// Accepts the company name
cout << "Enter company's name please:"<<endl;
cin>>temp.coname;
// Validates the number of shares entered by the user
do
{
// Accepts number of shares
cout << "enter the number of shares bought:"<<endl;
cin >> temp.numShares;
// Checks if the number of shares is zero or less than zero then show error message
if(temp.numShares <= 0)
cout << "ERROR: Invalid Share (Cannot be negative or zero)."<<endl;
// Otherwise valid data come out of the loop
else
break;
}while(1);// End of do - while loop
// Validates the purchase price entered by the user
do
{
// Accepts purchase price
cout <<"What was the purchase price?"<<endl;
cin >> temp.PurPrice;
bool is_negative = temp.PurPrice <= 0.0f;
// Checks if the purchase price is zero or less than zero then show error message
if(is_negative)
cout << "ERROR: Invalid Purchase Price (Cannot be negative or zero)."<<endl;
// Otherwise valid data come out of the loop
else
break;
}while(1); // End of do - while loop
// Validates the current price entered by the user
do
{
// Accepts current price
cout << "what is the current price of the share?"<<endl;
cin >> temp.CurrPrice;
bool is_negative = temp.CurrPrice <= 0.0f;
// Checks if the current price is zero or less than zero then show error message
if(is_negative)
cout << "ERROR: Invalid Current Price (Cannot be negative or zero)."<<endl;
// Otherwise valid data come out of the loop
else
break;
}while(1);// End of do - while loop
cin.ignore();
// Returns the object
return temp;
}// End of function
// Function to calculate profit or loss and display stock information
void displayResult (struct StockInfo portfolio[], int size)
{
// To store profit or loss and total
double Profitloss, total = 0;
// Displays the heading
cout<<"Portfolio Report ================"<<endl;
cout<<"Company Profit(Loss)"<<endl;
// Loops till number of stocks
for(int i = 0; i < size; i++)
{
// Calculates profit or loss
Profitloss = portfolio[i].numShares * (portfolio[i].CurrPrice - portfolio[i].PurPrice);
// Displays stock information
cout<<portfolio[i].coname<<setw(15)<<"$"<<setw(5)<<Profitloss<<endl;
// Calculates total profit or loss
total += Profitloss;
}// End of for loop
// Checks if the total is greater than or equals to zero
// Then display profit message
if(total >= 0)
cout<<"Your total Profit is: $"<<total<<endl;
// Otherwise display loss message
else
cout<<"Your total Loss is: $"<<total<<endl;
}// End of function
// main function definition
int main()
{
// Declares a constant size for the stock array
const int size = 3;
// Declares a StockInfo array of object of size constant size
StockInfo portfolio[size];
// To store current record counter
int count = 0;
// To store the choice entered by the user
int choice;
// Loops till user choice is not 3
do
{
// Displays menu
cout << endl
<< " 1 - Add a stock. "
<< " 2 - Display Profit/loss. "
<< " 3 - Exit Program. "
<< " Enter your choice and press return: ";
// Accepts choice from the user
cin >> choice;
// Checks the choice value and calls the appropriate function based on the user choice
switch (choice)
{
case 1:
// Checks if the current record counter is less then the array size
if(count < size)
{
// Calls the function to return a record entered by the user
// Stores the return object to count index position of the array
portfolio[count] = addRecord ();
// Increase the record counter by one
count++;
}// End of if condition
// Otherwise display error message
else
cout<<" ERROR: Not enough space to store Stock Information.";
break;
case 2:
// Calls the function to calculate and display stock information
displayResult(portfolio, count);
break;
case 3:
//Exit the program
exit(0);
break;
default:
cout << "Not a Valid Choice. "<< "Choose again. ";
break;
}// End of switch case
}while(choice < 3); // End of do while loop
return 0;
}// End of main function
Sample Output:
1 - Add a stock.
2 - Display Profit/loss.
3 - Exit Program.
Enter your choice and press return: 1
Enter company's name please:
Amgen
enter the number of shares bought:
0
ERROR: Invalid Share (Cannot be negative or zero).
enter the number of shares bought:
200
What was the purchase price?
-2
ERROR: Invalid Purchase Price (Cannot be negative or zero).
What was the purchase price?
50.75
what is the current price of the share?
0
ERROR: Invalid Current Price (Cannot be negative or zero).
what is the current price of the share?
75.625
1 - Add a stock.
2 - Display Profit/loss.
3 - Exit Program.
Enter your choice and press return: 2
Portfolio Report
================
Company Profit(Loss)
Amgen $ 4975
Your total Profit is: $4975
1 - Add a stock.
2 - Display Profit/loss.
3 - Exit Program.
Enter your choice and press return: 1
Enter company's name please:
Delta
enter the number of shares bought:
100
What was the purchase price?
111.75
what is the current price of the share?
97.25
1 - Add a stock.
2 - Display Profit/loss.
3 - Exit Program.
Enter your choice and press return: 2
Portfolio Report
================
Company Profit(Loss)
Amgen $ 4975
Delta $-1450
Your total Profit is: $3525
1 - Add a stock.
2 - Display Profit/loss.
3 - Exit Program.
Enter your choice and press return: 1
Enter company's name please:
Cisco
enter the number of shares bought:
400
What was the purchase price?
22.0
what is the current price of the share?
16.50
1 - Add a stock.
2 - Display Profit/loss.
3 - Exit Program.
Enter your choice and press return: 2
Portfolio Report
================
Company Profit(Loss)
Amgen $ 4975
Delta $-1450
Cisco $-2200
Your total Profit is: $1325
1 - Add a stock.
2 - Display Profit/loss.
3 - Exit Program.
Enter your choice and press return: 1
ERROR: Not enough space to store Stock Information.
1 - Add a stock.
2 - Display Profit/loss.
3 - Exit Program.
Enter your choice and press return: 3
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.