d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Visual Studio Message Box
12Next
Add Reply New Topic New Poll
Member
Posts: 6,175
Joined: Sep 4 2009
Gold: Locked
Trader: Scammer
Nov 1 2014 04:08pm
quick question, how do I make a list with a message box?

I've been only taught this method which is: MessageBox.Show ( " Initial Message ", "Title", MessageBoxButton.() , MessageBoxIcon.() );

However for my assignment, it tells me to format it like this




MessageBox.Show(a.ToString("C"), "Compound Interest", MessageBoxButtons.OK, MessageBoxIcon.Information);
This is what I have at the moment, but it only shows one message box at a time.

Is there any way to make it so all my calculations go into one message box?

This is my coding: A person invests $1000.00 in a savings account that yields 5% interest annually

const decimal r = 0.05m;
decimal p = 1000;
decimal a;
int n = 1;

while (n <= 5)
{
p = p + (r * p);
a = p;
n++;
MessageBox.Show(a.ToString("C"), "Compound Interest", MessageBoxButtons.OK, MessageBoxIcon.Information);

}

Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 1 2014 05:21pm
have you tried string concatenation?
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Nov 1 2014 05:47pm
Quote (carteblanche @ Nov 1 2014 07:21pm)
have you tried string concatenation?


This. Inside your loop concat all your values into a single string with newlines placed after each result. Then you can further format/display it from there.
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Nov 1 2014 06:28pm
Your interest calculation is incorrect as well. To add 5% of the principal to the principal you simply do

Code
p = p * (1 + r);


Member
Posts: 6,175
Joined: Sep 4 2009
Gold: Locked
Trader: Scammer
Nov 1 2014 08:59pm
Quote (AbDuCt @ Nov 1 2014 03:47pm)
This. Inside your loop concat all your values into a single string with newlines placed after each result. Then you can further format/display it from there.


thanks for replying! I've been trying to concatenate the strings, but I can't seem to do it. how do i store each iteration into one string? Other than that, I understand how to format the message box now
Member
Posts: 6,175
Joined: Sep 4 2009
Gold: Locked
Trader: Scammer
Nov 1 2014 09:01pm
Quote (Minkomonster @ Nov 1 2014 04:28pm)
Your interest calculation is incorrect as well. To add 5% of the principal to the principal you simply do

Code
p = p * (1 + r);


yeah, the exact formula I was given was a = p * (1 + r) ^ n ), but I didn't quite understand the 1+r part so I made my own equation, which also worked

To be precise:

a = original deposit
r = interest rate
n = # of years
p = balance
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Nov 1 2014 09:18pm
Quote (Chesse @ Nov 1 2014 10:59pm)
thanks for replying! I've been trying to concatenate the strings, but I can't seem to do it. how do i store each iteration into one string? Other than that, I understand how to format the message box now


Just append the string each iteration:

Code
string myString;
const decimal r = 0.05m;
decimal p = 1000;
decimal a;
int n = 1;

while (n <= 5)
{
p = p + (r * p);
myString += p.toString + "\n";
n++;
}

MessageBox.Show(myString, "Compound Interest", MessageBoxButtons.OK, MessageBoxIcon.Information);


This post was edited by AbDuCt on Nov 1 2014 09:18pm
Member
Posts: 6,175
Joined: Sep 4 2009
Gold: Locked
Trader: Scammer
Nov 1 2014 11:40pm
Quote (AbDuCt @ Nov 1 2014 07:18pm)
Just append the string each iteration:

Code
string myString;
const decimal r = 0.05m;
decimal p = 1000;
decimal a;
int n = 1;

while (n <= 5)
{
p = p + (r * p);
myString += p.toString + "\n";
n++;
}

MessageBox.Show(myString, "Compound Interest", MessageBoxButtons.OK, MessageBoxIcon.Information);


I tried that, but its still giving me separate message boxes for each iteration
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Nov 1 2014 11:42pm
Then you aren't doing that. You have the MessageBox.Show inside the while loop. Move it outside of the while loop like shown above.
Member
Posts: 6,175
Joined: Sep 4 2009
Gold: Locked
Trader: Scammer
Nov 1 2014 11:51pm
Quote (Minkomonster @ Nov 1 2014 09:42pm)
Then you aren't doing that. You have the MessageBox.Show inside the while loop. Move it outside of the while loop like shown above.


It gives me this : "use of unassigned local variable" when i move it outside
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll