I need help getting the out put to read two decm. points for the dollar amount o
ID: 3665421 • Letter: I
Question
I need help getting the out put to read two decm. points for the dollar amount of stocks with market price.
Here is the question:
The price of stocks is sometimes given to the nearest eighth of a dollar; for example, 297/8 or 891/2. Write a program that computes the value of the the user's holding stock. The program ask for the number od shares of stock owned, the whole-dollar portion of the price, and the faction portion. The faction portion is to be input as two int values, one for the numerator and one for the denominator. The program then outputs the value of the user's holdings. Your program should allow the user to repeat this calculation as often as the user wishes and will include a fuction definition that has three int arguments consisting of the whole-dollar portion of the price and the two integers that make up the faction part. The function returns the price of one share of stock as a single number type double.
Here is my code:
#include <iostream>
using namespace std;
// Function prototype.
double StockPrice (int whole, int nr, int dr, int stock);
int main ()
{
// Declare virables.
int whole, nr, dr, stock;
double stockVal = 0;
char Continue;
// Begain loop.
do
{
// Input whole part.
cout<<"Enter stock price and number of shares, please."<<endl;
cout<<"Enter price as integers: dollars, numberator, denominator"<<endl;
cin>>whole;
cin>>nr;
cin>>dr;
// Input of stocks.
cout<<"Enter number of shares held. "<<endl;
cin>>stock;
// Call function.
stockVal = StockPrice (whole, nr, dr, stock);
cout<<""<<stock<< " shares of stock with market price "<<whole<<" "<<nr<<"/"<<dr<<endl;
cout<<"Have a of value $ "<<stockVal<<endl;
cout<<"Press 'y' or 'Y' to repeat calculations for different variables? "<<endl;
cin>>Continue;
}
while (Continue == 'y' || Continue == 'Y');
// Pause system for a while.
system("pause");
}
// Function definition.
double StockPrice (int whole, int nr, int dr, int stock)
{
double price;
price=static_cast<double>(whole * dr + nr)/(dr * stock);
return price ;
}
Here is what the output should look like:
Explanation / Answer
I have made the following changes:
#include <iostream>
using namespace std;
// Function prototype.
double StockPrice (int whole, int nr, int dr, int stock);
int main ()
{
// Declare virables.
int whole, nr, dr, stock;
double stockVal = 0;
char Continue;
// Begin loop.
do
{
// Input whole part.
cout<<"Enter stock price and number of shares, please."<<endl;
cout<<"Enter price as integers: dollars, numberator, denominator"<<endl;
cin>>whole;
cin>>nr;
cin>>dr;
// Input of stocks.
cout<<"Enter number of shares held. "<<endl;
cin>>stock;
// Call function.
stockVal = StockPrice (whole, nr, dr, stock);
cout<<""<<stock<< " shares of stock with market price "<<whole<<" "<<nr<<"/"<<dr<<endl;
cout<<"Have a of value $ "<<stockVal<<endl;
cout<<"Press 'y' or 'Y' to repeat calculations for different variables? "<<endl;
cin>>Continue;
} while (Continue == 'y' || Continue == 'Y');
// Pause system for a while.
system("pause");
}
// Function definition.
double StockPrice (int whole, int nr, int dr, int stock)
{
double price;
price=((whole * dr) + nr))/(dr * stock); /* you can directly assign an integer to a double in c++, there's no need to do an explicit casting.*/
return price ;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.