c practice problem Write a program that uses a structure for
c++ practice problem.
Write a program that uses a structure for storing the name of a stock, its estimated earnings per share, and its estimated price-to-earnings ratio. Have the program prompt the user to enter these items for four different stocks, each time using the same structure to store the entered data. When the data has been entered for a particular stock, have the program compute and display the stock price anticipated on the basis of the entered earnings and price-per-share values. For example, if a user entered the data: ABC 1. 14 19 The anticipated price for a share of ABC stock would be 1.14*19 = $21.66.Solution
#include <iostream>
#include <string>
using namespace std;
//structure to hold stock details
struct stock {
string name;
double price;
}sample ;
int main()
{
//variables to hold use data
string name;
int no;
double price;
//loop for 4 times to prompt user to enter stocks data
for(int i=0;i<4;i++)
{
//prompt user for stock data
cout << \"Enter stock details in the format NAME PRICE NO_SHARES\" << endl;
cout<<\"Input: \";
cin>>name>>price>>no;
//assign entered into stock sttructure
sample.name=name;
sample.price=price * no;
//display price of stocks
cout<<\"The anticipated price for a share of \"<<sample.name<< \" stock would be \"<<price <<\" * \"<<no<<\" = $\"<<sample.price<<\".\ \";
}
return 0;
}
