I am soooo lost. Please help me. I have to write a program which uses an inline function calculateStockValue. the function takes two parameters - one is a number of shares and the other is unit price of a share. User should be able to use this function as many times as he wants unless he wants to quit. (loop here???) Once user has completed entering data, the program should be able to provide a feedback to the user about his total account value (sum of all the values of the shares). It says I also have to write a test program (main function) to test the working of the code.
There's so much of this I don't understand. So, for starters, how do I accumulate the stock shares and their related prices?
//inline.cpp -- using an inline function
#include <iostream>
usingnamespace std;
//an inline function definition
inlinefloat calculatedStockValue(unsignedint shares, float price)
{
return(shares*price);
}
main ()
{
unsignedint Portfolio (void)
unsignedint StockShares;
float StockPrice;
float value = StockShares * StockPrice;
cout<<"Please enter the number of shares of your first stock: ";
cin>>StockShares;
cout<<"Please enter the price of the stock: ";
cin>>StockPrice;
cout<<"Your total stock value is "<<calculatedStockValue();
return ();
}
Yes, you would have the function call in a loop. To store the value, simply make a float variable outside of the loop initialized to 0, and the inside the loop whenever the function is called add the result to the variable.
Oh my goodness, thank you so much for replying!!! I have put in so much time on this and have no one to lead me in the right direction.
So, I added a loop below and what I think is an exit for the loop. I'm still getting fail error. I know this code is messed up. Any advice is greatly appreciated.
//inline.cpp -- using an inline function
#include <iostream>
usingnamespace std;
//an inline function definition
inlinefloat calculatedStockValue(unsignedint shares, float price)
{
return(shares*price);
}
main ()
{
unsignedint Portfolio (void)
unsignedint StockShares;
float StockPrice;
float value = StockShares * StockPrice;
do
{
cout<<"Please enter the number of shares of your first stock: ";
cin>>StockShares;
cout<<"Please enter the price of the stock: ";
cin>>StockPrice;
char choice;
cout << "Would you like to enter a new stock ? [y/n] ";
cin >> choice;
while (choice == 'y' || choice == 'Y');
}
cout<<"Your total stock value is "<<calculatedStockValue();
return ();
}