/* Lab 36 In this program the user will enter the following information for a po
ID: 3830500 • Letter: #
Question
/* Lab 36
In this program the user will enter the following information for a portfolio of
stocks (up to the maximum number of stocks indicated by MAX_STOCKS):
Stock Symbol
Number of Shares
Stock Price
The information that the user enters must be stored in parallel arrays (one array
for each of the three pieces of information listed above). When the user says they are
done entering stock information -OR- they have entered the maximum number of stocks, then
a chart must be produced on the screen showing each stock's symbol, the number of shares
owned, the current price of the stock and the total value of ownership interest in that
stock.
In addition to displaying the stock portfolio chart, the program also needs to:
- determine which stock has the highest price and display the symbol and price of that stock
- calculate (using an accumulator) and disply the total value of the stock portfolio
To complete this lab, follow the TODO steps listed below. Remember that each part of
this lab is worth points, so be sure to complete everything that you know how to do
as quickly as you can
*/
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
//------------------------------------------------------------------------------
int main()
{
const int MAX_STOCKS = 10;
/* TODO - Step 1
Declare 3 arrays, with the data types listed below - each
holding as many values as determined by the constant declared above:
- array of string, for the stock symbols
- array of int, for the number of shares owned of that stock
- array of double, for the current price of the stock
*/
// Parallel array declarations
// These variables will hold array index values
int currentStockIndex = 0; // used when user enters data to retain current array index
int indexOfTopStockPrice = 0; // array index that points to the stock with the highest price
// Loop control variable, holds 'y' if user wants to add another stock to their portfolio
char again;
// Accumulator variable
double totalValueOfAllStocks = 0.0; // variable used to total up the value of all stocks owned
/* TODO - Step 2
Complete the DO...WHILE loop that has been started here (below this comment).
You will need to scroll DOWN, after STEP 10, where the following line is commented out:
//} while (expression);
Uncomment the line and change "expression" so the loop repeats if:
- the user indicated they want to add another stock, AND
- fewer than MAX_STOCKS have been added
*/
do
{
/* TODO - Step 3
Output prompt to tell user which stock they are entering information for
*/
/* TODO - Step 4
- Prompt the user for the stock's symbol
- Read the user's answer and store to the correct array in the correct location
*/
/* TODO - Step 5
- Prompt the user for this stock's current price
- Read the user's answer and store to the correct array in the correct location
*/
/* TODO - Step 6
Write a WHILE loop to validate the user's entry. If the user's entry for the stock
price was not more than 0, then:
- Display an error message (example in 2nd stock Sample Output below)
- Read the user's answer again and store to the correct array in the same location
*/
/* TODO - Step 7
- Prompt the user for the number of shares owned of this stock
- Read the user's answer and store to the correct array in the correct location
*/
/* TODO - Step 8
Write a WHILE loop to validate the user's entry. If the user's entry for the number of
shares owned was 0 or less or more than 10000, then:
- Display an error message (example in 3rd stock Sample Output below)
- Read the user's answer again and store to the correct array in the same location
*/
/* TODO - Step 9
Increment the index that is being used for the parallel arrays, readying it for
the next time around loop
*/
/* TODO - Step 10
If the parallel array index is still less than the max number of stocks allowed
(as identified by the MAX_STOCKS constant), then
- ask the user if they want to enter another stock
- read the user's answer from the keyboard
- convert the user's answer to upper case and store it back to the loop control variable (again)
*/
cout << endl;
//=======================================================================
// This is the spot that Step 2 told you to look for and change
//=======================================================================
//} while (expression);
// END OF THE DO...WHILE LOOP
// Display the header for the chart
cout << endl;
cout << "Stock Qty Price Value" << endl;
cout << "=================================" << endl;
cout << fixed;
/* TODO - Step 11
Write a for loop that will loop based on the number of stocks entered above.
You will need to declare a loop control variable, it has NOT been provided
*/
//for (initialize; test; increment)
{
/* TODO - Step 12
Calculate the total value of the current stock
Total Stock Value = # of shares <multiplied by> stock price
NOTE: You will need to declare a variable here, with an appropriate data
type, to hold the value of the current stock. Store the result of your
calculation in this variable.
*/
/* TODO - Step 13
Output the current stock's symbol, quantity owned, current price and total value
as a single line to the console. Use setw and any necessary output manipulator(s)
to exactly match the Sample Output.
You MAY use two blank space string literals and two $ literals, ALL other alignment
MUST be achieved using setw properly.
*/
/* TODO - Step 14
If the price of the current stock is higher than the stock price
pointed to by indexOfTopStockPrice, then
- save the current loop index as the new indexOfTopStockPrice
*/
/* TODO - Step 15
Use the accumulator variable declared at the top of the program
to sum up the total value of all the stocks owned (by adding the
value of the current stock to that total)
*/
// End of the for loop
}
/* TODO - Step 16
Display the line identifying the stock with the highest price
(as determined in the loop above)
*/
/* TODO - Step 17
Display the line showing the total value of the stocks
(as calculated in the loop above)
*/
return 0;
}
/* Sample Output
Enter #1 Stock Info
Stock Symbol: DIS
Current Price: $115.60
Shares Owned: 1000
Do you want to enter another stock (y/n): y
Enter #2 Stock Info
Stock Symbol: CMCS
Current Price: $0
Error: Stock price must be more than $0. Enter again: $-35.69
Error: Stock price must be more than $0. Enter again: $35.69
Shares Owned: 223
Do you want to enter another stock (y/n): y
Enter #3 Stock Info
Stock Symbol: AAPL
Current Price: $143.65
Shares Owned: 0
Shares owned must be 1-10,000. Re-enter: -1
Shares owned must be 1-10,000. Re-enter: 100000
Shares owned must be 1-10,000. Re-enter: 523
Do you want to enter another stock (y/n): y
Enter #4 Stock Info
Stock Symbol: MSFT
Current Price: $68.46
Shares Owned: 721
Do you want to enter another stock (y/n): y
Enter #5 Stock Info
Stock Symbol: BRKB
Current Price: $3176.90
Shares Owned: 225
Do you want to enter another stock (y/n): n
Stock Qty Price Value
=================================
DIS 1000 $ 115.60 $ 115600.00
CMCS 223 $ 35.69 $ 7958.87
AAPL 523 $ 143.65 $ 75128.95
MSFT 721 $ 68.46 $ 49359.66
BRKB 225 $ 3176.90 $ 714802.50
The stock with the highest price is: BRKB ($3176.90).
Total value = $962849.98
Press any key to continue . . .
*/
Explanation / Answer
Here is the code for you:
/* Lab 36
In this program the user will enter the following information for a portfolio of
stocks (up to the maximum number of stocks indicated by MAX_STOCKS):
Stock Symbol
Number of Shares
Stock Price
The information that the user enters must be stored in parallel arrays (one array
for each of the three pieces of information listed above). When the user says they are
done entering stock information -OR- they have entered the maximum number of stocks, then
a chart must be produced on the screen showing each stock's symbol, the number of shares
owned, the current price of the stock and the total value of ownership interest in that
stock.
In addition to displaying the stock portfolio chart, the program also needs to:
- determine which stock has the highest price and display the symbol and price of that stock
- calculate (using an accumulator) and disply the total value of the stock portfolio
To complete this lab, follow the TODO steps listed below. Remember that each part of
this lab is worth points, so be sure to complete everything that you know how to do
as quickly as you can
*/
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
//------------------------------------------------------------------------------
int main()
{
const int MAX_STOCKS = 10;
/* TODO - Step 1
Declare 3 arrays, with the data types listed below - each
holding as many values as determined by the constant declared above:
- array of string, for the stock symbols
- array of int, for the number of shares owned of that stock
- array of double, for the current price of the stock
*/
// Parallel array declarations
string stockSymbols[MAX_STOCKS];
int numOfSharesOwned[MAX_STOCKS];
double currentPrice[MAX_STOCKS];
// These variables will hold array index values
int currentStockIndex = 0; // used when user enters data to retain current array index
int indexOfTopStockPrice = 0; // array index that points to the stock with the highest price
// Loop control variable, holds 'y' if user wants to add another stock to their portfolio
char again;
// Accumulator variable
double totalValueOfAllStocks = 0.0; // variable used to total up the value of all stocks owned
/* TODO - Step 2
Complete the DO...WHILE loop that has been started here (below this comment).
You will need to scroll DOWN, after STEP 10, where the following line is commented out:
//} while (expression);
Uncomment the line and change "expression" so the loop repeats if:
- the user indicated they want to add another stock, AND
- fewer than MAX_STOCKS have been added
*/
int i = 0;
do
{
/* TODO - Step 3
Output prompt to tell user which stock they are entering information for
*/
cout << "Enter #" << i + 1 << " Stock Info" << endl;
/* TODO - Step 4
- Prompt the user for the stock's symbol
- Read the user's answer and store to the correct array in the correct location
*/
cout << "Stock Symbol: ";
cin >> stockSymbols[i];
/* TODO - Step 5
- Prompt the user for this stock's current price
- Read the user's answer and store to the correct array in the correct location
*/
cout << "Current Price: $";
cin >> currentPrice[i];
/* TODO - Step 6
Write a WHILE loop to validate the user's entry. If the user's entry for the stock
price was not more than 0, then:
- Display an error message (example in 2nd stock Sample Output below)
- Read the user's answer again and store to the correct array in the same location
*/
while(currentPrice[i] <= 0)
{
cout << "Error: Stock price must be more than $0. Enter again: $";
cin >> currentPrice[i];
}
/* TODO - Step 7
- Prompt the user for the number of shares owned of this stock
- Read the user's answer and store to the correct array in the correct location
*/
cout << "Shares Owned: ";
cin >> numOfSharesOwned[i];
/* TODO - Step 8
Write a WHILE loop to validate the user's entry. If the user's entry for the number of
shares owned was 0 or less or more than 10000, then:
- Display an error message (example in 3rd stock Sample Output below)
- Read the user's answer again and store to the correct array in the same location
*/
while(numOfSharesOwned[i] < 1 || numOfSharesOwned[i] > 10000)
{
cout << "Shares owned must be 1-10,000. Re-enter: ";
cin >> numOfSharesOwned[i];
}
/* TODO - Step 9
Increment the index that is being used for the parallel arrays, readying it for
the next time around loop
*/
i++;
/* TODO - Step 10
If the parallel array index is still less than the max number of stocks allowed
(as identified by the MAX_STOCKS constant), then
- ask the user if they want to enter another stock
- read the user's answer from the keyboard
- convert the user's answer to upper case and store it back to the loop control variable (again)
*/
char again = 'N';
if(i < MAX_STOCKS)
{
cout << "Do you want to enter another stock (y/n): ";
cin >> again;
again = toupper(again);
}
cout << endl;
//=======================================================================
// This is the spot that Step 2 told you to look for and change
//=======================================================================
} while (again != 'N');
// END OF THE DO...WHILE LOOP
// Display the header for the chart
cout << endl;
cout << "Stock Qty Price Value" << endl;
cout << "=================================" << endl;
cout << fixed;
/* TODO - Step 11
Write a for loop that will loop based on the number of stocks entered above.
You will need to declare a loop control variable, it has NOT been provided
*/
//int indexOfTopStockPrice = 0;
double grandTotal = 0;
for (int j = 0; j < i; j++)
{
/* TODO - Step 12
Calculate the total value of the current stock
Total Stock Value = # of shares <multiplied by> stock price
NOTE: You will need to declare a variable here, with an appropriate data
type, to hold the value of the current stock. Store the result of your
calculation in this variable.
*/
double totalStockValue = numOfSharesOwned[j] * currentPrice[j];
/* TODO - Step 13
Output the current stock's symbol, quantity owned, current price and total value
as a single line to the console. Use setw and any necessary output manipulator(s)
to exactly match the Sample Output.
You MAY use two blank space string literals and two $ literals, ALL other alignment
MUST be achieved using setw properly.
*/
cout << setw(8) << stockSymbols[j] << " " << setw(8) << numOfSharesOwned[j];
cout << "$" << fixed << setprecision(2) << currentPrice[j];
cout << "$" << fixed << setprecision(2) << totalStockValue << endl;
/* TODO - Step 14
If the price of the current stock is higher than the stock price
pointed to by indexOfTopStockPrice, then
- save the current loop index as the new indexOfTopStockPrice
*/
if(currentPrice[j] > currentPrice[indexOfTopStockPrice])
indexOfTopStockPrice = j;
/* TODO - Step 15
Use the accumulator variable declared at the top of the program
to sum up the total value of all the stocks owned (by adding the
value of the current stock to that total)
*/
grandTotal += totalStockValue;
// End of the for loop
}
/* TODO - Step 16
Display the line identifying the stock with the highest price
(as determined in the loop above)
*/
cout << "The stock with the highest price is: " << stockSymbols[indexOfTopStockPrice];
cout << " ($" << fixed << setprecision(2) << currentPrice[indexOfTopStockPrice] << ")." << endl;
/* TODO - Step 17
Display the line showing the total value of the stocks
(as calculated in the loop above)
*/
cout << "Total value = $" << fixed << setprecision(2) << grandTotal << endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.