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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>          // 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 <iostream>          // 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.