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 ();
}
inlinefloat calcualteStockValue(unsignedint shares,float price)
{
return shares*price;
}
main
declare variables
int shares;
float price;
char response;
intialize total=0;
do
{
get shares and price from user;(cin)
calcualte stock value and add it to total
ask user does he want continue;
get response from user(cin)
response=tolower(response);
}while(response=='y');
output total;
With your suggestions, I've changed it to this below. Can you tell me why it is telling me that line 20, total, is an error, expression must have integer or enum type?
#include <iostream>
usingnamespace std;
inlinefloat calculateStockValue(unsignedint shares,float price)
{
return shares*price;
}
int main ()
{
int shares;
float price;
char response;
float total=0;
do
{
cout<<"Please enter the number of shares of your first stock: ";
cin>>shares;
cout<<"Please enter the price of the stock: ";
cin>>price;
total = calculateStockValue + total;
cout << "Would you like to enter a new stock ? [y/n] ";
cin >> response;
response=tolower(response);
}
while(response=='y');
cout<<"The current value of your Stock Portfolio is "<<total;
return 0;
You did it! You got it working!!! WOW...thank you soooo very much.
Last question if you don't mind. Part of the lab says I also have to write a test program (main function) to test the working of the code.
I have no idea what that actually means. Isn't the main function already running the inline function in the code? Is that what those instructions sound like to you? Or is there something else I'm supposed to include?
Sounds like he just wants you to thouroughly test the code. I don't really know what more he would want other than what you have; it seems to pretty much do what it's supposed to.