The code keeps returning the values from the default constructor, instead of returning the values that were inputted by the user.
So say the user inputs ABCD and 99 for the symbol and shareprice. I'm able to show the value with aStock.displaystock();. But when i do it through the class StockPurchase, it keeps returning the value that is in the default constructor.
I've looked through my textbook and online and i can't find anything on how to fix it.
This is where the problem happens.
1 2 3 4 5 6
void StockPurchase::displayStockPurchase()
{
object.displayStock();
cout << "The number of shares you want to purchase is " << sharestopurchase << endl;
cout << "The total cost of your purchase is " << getCost() << endl;
So for object.displayStock();
It is able to return the cout messages from displayStock();
but it does not return the values of symbol and shareprice. Instead it shows the values put in the default constructor. Which is
int main()
{
Stock aStock;
string symbo;
double pric;
StockPurchase aPurchase;
int amt;
cout << "Please enter the symbol of your stock." << endl;
cin >> symbo;
cout << "Please enter the share price of your stock." << endl;
cin >> pric;
aStock.setStock(symbo, pric);
cout << "Please enter the amount of shares you want to purchase." << endl;
cin >> amt;
aPurchase.setSharestoPurchase(amt);
aPurchase.displayStockPurchase();
cout << "Please press enter to continue." << endl;
cin.ignore(2);
return 0;
}
Its just in the main(). I have the user input values and then i call the display functions.
The object named aStock in main() and the member StockPurchace::object of the StockPurchace instance named aPurchase in main() are unrelated. When you call aStock.setStock(symbo, pric) you're only modifying the Stock instance in main(), not the class member. StockPurchase only modifies its Stock member when parameters are passed to its constructors, but you default-construct it on line 6. You'll either have to wait until line 16 to create the StockPurchase and then call the constructor that takes four parameters, or you'll have to add a function to StockPurchase that calls Stock::setStock().
int main()
{
string symbo;
double pric;
int amt;
cout << "Please enter the symbol of your stock." << endl;
cin >> symbo;
cout << "Please enter the share price of your stock." << endl;
cin >> pric;
cout << "Please enter the amount of shares you want to purchase." << endl;
cin >> amt;
StockPurchase aPurchase(amt, symbo, pric);
aPurchase.displayStockPurchase();
cout << "Please press enter to continue." << endl;
cin.ignore(2);
return 0;
}
I am able to call the display function and it shows the user inputs now, instead of the default constructor values.