n C++ and with comments please PROBLEM: Create a menu driven application to keep
ID: 3872826 • Letter: N
Question
n C++ and with comments please
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 50.75
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:
Data validation is optional. I will only test with valid data.
***USE FUNCTIONS to organize your code!
Main should have the loop to control the menu and calls to functions for each option:
addRecord needs the array and the next available poosition to add an element
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!)
Explanation / Answer
Here is the code i hope it will help you
#include<stdio.h>
#include<stdlib.h>
struct StockInfo{
char Company[25];
int Shares;
double CurPrice;
double PurPrice;
double CurValue; //all parameters declared in structure
}s[10];
void addstock(int c) // function to add stocks
{
printf("Print Values ");
printf("Enter Company Name :");
scanf("%s",s[c].Company);
printf("Number of Shares? ");
scanf("%d", &s[c].Shares);
printf("Purchase Price? ");
scanf("%lf", &s[c].PurPrice);
printf("Current Price? ");
scanf("%lf", &s[c].CurPrice);
s[c].CurValue=s[c].Shares*(s[c].CurPrice-s[c].PurPrice); // current value is set
}
void display(int c) // function for display
{
int i;
double total=0;
printf("Portfolio Report ");
printf("========================= ");
printf("Company Profit(Loss) ");
for(i=0;i<c;i++)
{
total+=s[i].CurValue;
printf("%s $%lf ",s[i].Company,s[i].CurValue);
}
printf("Total %lf ",total);
}
int main()
{
int c=0; // to get the number of stocks
int n;
char ch;
do{ // we use do while for continue (y/n)
printf("***************MENU*************** ");
printf("1.Add Stock 2.Display Profit/Loss 3.Exit Program ");
scanf("%d",&n);
switch(n) // switch is better for menu
{
case 1 : printf("Add stock");
if(c>=10)
printf("Stock Array full ");
else
{
addstock(c);
c++;
}
break; // to stop
case 2 : display(c);
break;
case 3 : exit(0); // exit the program it requires <stdlib.h>
}
printf("continue(y/n)");
while(getchar()!=' ');
scanf("%c",&ch);
}while(ch=='y' || ch == 'Y'); // cheaks the user input
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.