### Constant Loop/Interest question

I have a homework question that asks the following:

Write a C++ program that calculates a portfolio rise and fall over a number of market sessions. At each step of the way, you should display the new value of your hypothetical portfolio. During your bull markets, increase your portfolio by 20%. During your bear market, decrease your portfolio by 20%. In order to receive full credit, you must use a loop. IN ORDER TO RECEIVE FULL CREDIT, YOUR PROGRAM MUST DECLARE AND USE A const QUANTITY. The program dialogue should look exactly like this:

Enter a starting portfolio size:1000

After a bull market, your portfolio has grown to:1200.00

After a bear market, your portfolio has shrunk to:800.00

Continue(1=yes/0=no)? 1

Was it a bull or bear market (1=bull/0=bear)?1

Your portfolio is now 1200.00
After a bull market, your portfolio has grown to:1440.00

After a bear market, your portfolio has shrunk to:980.00

Continue(1=yes/0=no)? 1
Was it a bull or bear market (1=bull/0=bear)?0

Your portfolio is now 980.00
After a bull market, your portfolio has grown to:1176.00

After a bear market, your portfolio has shrunk to:784.00

Continue(1=yes/0=no)? 0
Was it a bull or bear market (1=bull/0=bear)?1

Your final portfolio is 1176.00

I have gathered that the const is going to be the interest of .2

How do I set up coding to show each scenario of interest added, or lost in a bull v. bear market and then prompt the user to continue and go through the method again??

Thanks

So far this is my code
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657`` ``````#include // for std::cout and std::cin using namespace std; // supports cout and cin // This program calculates a portfolio rise and fall // over a number of market sessions int main () { #include // for std::cout and std::cin using namespace std; // supports cout and cin // This program calculates a portfolio rise and fall // over a number of market sessions int main () { int port_size, ans, market; const double interest=.2; cout << "Enter a starting portfolio size:"; cin >> port_size; while (ans = 1) { cout << "After a bull market, your portfolio has grown to:" << (port_size + port_size*interest) << endl; cout << "After a bear market, your portfolio has shrunk to:" << (port_size - port_size*interest) << endl; cout << "Continue(1=yes/0=no)?"; cin >> ans; cout << "Was it a bull or bear market (1=bull/0=bear)?"; cin >> market; if (market == 1) { cout << "Your portfolio is now " << (port_size + port_size*interest) << endl; } else { cout << "Your portfolio is now " << (port_size - port_size*interest) << endl; } } if (market == 1) { cout << "Your portfolio is now " << (port_size + port_size*interest) << endl; } else { cout << "Your portfolio is now " << (port_size - port_size*interest) << endl; } return 0; } }``````
Last edited on
Your while loop should have ans == 1, not ans = 1.

When declaring ans, you should declare it to be one. int ans = 1;

You shouldn't need the second if statement from line 45 - line 54.

Based on the example, once the user is done it would state "Your final portfolio...", in this case I would do an if statement to check if the user is done or not.
if(ans == 1)
{
//portfolio is
}
else
{
//final portfolio is
}
Topic archived. No new replies allowed.