C
C#12mo ago
Aubrey

Help with adding something up

Need to add up the total amount PAID, im not sure how though
No description
33 Replies
Aubrey
AubreyOP12mo ago
namespace KreditRechner
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
double interest, loan, monthly, durationY, durationM, interestrate,
month=1;

interestrate= Convert.ToDouble(textBoxInterest.Text);
loan = Convert.ToDouble(textBoxloan.Text);
monthly = Convert.ToDouble(textBoxMonthly.Text);

listBox1.Items.Clear();
listBox1.Items.Add(month + ". Month : " + loan + "€");


while (loan > monthly)
{

interest = loan * (interestrate / 100 / 12);
loan = loan - monthly + interest;



month++;
listBox1.Items.Add(month + ". Month : " + Math.Round(loan, 2) + "€");

}

listBox1.Items.Add(month + ". Month : " + 0 + "€");

durationY = month % 12;
durationM = month / 12;

listBoxDuration.Items.Clear();
listBoxDuration.Items.Add(durationY + " years and ");
listBoxDuration.Items.Add(Math.Round(durationM, 0) + " months");


listBoxTotal.Items.Clear();
listboxTotal.Items.Add();



}
}
}
namespace KreditRechner
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
double interest, loan, monthly, durationY, durationM, interestrate,
month=1;

interestrate= Convert.ToDouble(textBoxInterest.Text);
loan = Convert.ToDouble(textBoxloan.Text);
monthly = Convert.ToDouble(textBoxMonthly.Text);

listBox1.Items.Clear();
listBox1.Items.Add(month + ". Month : " + loan + "€");


while (loan > monthly)
{

interest = loan * (interestrate / 100 / 12);
loan = loan - monthly + interest;



month++;
listBox1.Items.Add(month + ". Month : " + Math.Round(loan, 2) + "€");

}

listBox1.Items.Add(month + ". Month : " + 0 + "€");

durationY = month % 12;
durationM = month / 12;

listBoxDuration.Items.Clear();
listBoxDuration.Items.Add(durationY + " years and ");
listBoxDuration.Items.Add(Math.Round(durationM, 0) + " months");


listBoxTotal.Items.Clear();
listboxTotal.Items.Add();



}
}
}
SinFluxx
SinFluxx12mo ago
Well you already have a while loop that keeps running until you reach 0, so in that loop you should keep summing up the monthly payment and interest accrued each month at the same time to a total variable You even know what the total loan amount is so you could start it at that and then just add the interest each loop
Aubrey
AubreyOP12mo ago
thats the part that confuses me im not sure how to do it
SinFluxx
SinFluxx12mo ago
Think about what you're doing with your loan variable inside the while loop, and how it's really not all that different
Aubrey
AubreyOP12mo ago
why does it do this when its outside the while loop
No description
SinFluxx
SinFluxx12mo ago
Did you declare your total variable inside the while loop?
Aubrey
AubreyOP12mo ago
yes
SinFluxx
SinFluxx12mo ago
That's why then
Aubrey
AubreyOP12mo ago
its what im supposed to do, no?
SinFluxx
SinFluxx12mo ago
No, if you declare it inside the loop a) it's restricted to that scope, and b) it will reset to 0 on each iteration of the loop so won't actually get your total You should declare it before the loop, same as you did with loan
Aubrey
AubreyOP12mo ago
so this will work?
No description
SinFluxx
SinFluxx12mo ago
Give it a go
Aubrey
AubreyOP12mo ago
definitely removed the error still confused on the total though
SinFluxx
SinFluxx12mo ago
Why?
Aubrey
AubreyOP12mo ago
dont know how to get it 😭
SinFluxx
SinFluxx12mo ago
What do you mean "get it"?
Aubrey
AubreyOP12mo ago
the total amount
SinFluxx
SinFluxx12mo ago
That's not what I asked
Aubrey
AubreyOP12mo ago
well how to calculate it i always get some low number, which doesnt quite make sense
SinFluxx
SinFluxx12mo ago
What's the code you've added for it?
Aubrey
AubreyOP12mo ago
ive tried
total = loan + interest
total = loan + interest
inside the loop but that didnt work
SinFluxx
SinFluxx12mo ago
Well, that should give you a big number, too big a number, because you're adding the remaining loan amount every time
Aubrey
AubreyOP12mo ago
it gave me a number below 10000 like 600 something
SinFluxx
SinFluxx12mo ago
Yes sorry I just reread it, so there are two issues, you're adding the loan amount and you're not taking into account the existing total value For example when you're calculating the loan amount you start with loan = loan... Working out this specific line isn't particularly a programming problem, think about how you would do it if you were keeping track of the total you paid each month in a notebook or something
Aubrey
AubreyOP12mo ago
total = total + loan would give me a huge number
SinFluxx
SinFluxx12mo ago
I already said adding loan was wrong? Why would you add loan each time?
Aubrey
AubreyOP12mo ago
im slow
SinFluxx
SinFluxx12mo ago
If you wanted to include the loan then you would start the total amount at the loan amount once, before the while loop, then just add the interest each time in the loop OR You would start total at 0 and add the monthly amount and interest amount inside the loop
Aubrey
AubreyOP12mo ago
OH MY GOD THANK YOU THANK YOU SO MUCH IT WORKS
SinFluxx
SinFluxx12mo ago
You're welcome blobthumbsup honestly though you had done a lot of similar things already, so in theory you could have figured it out (though don't be afraid to ask for help), so perhaps just try and break things down into smaller chunks/write things down etc to help you figure them out
Aubrey
AubreyOP12mo ago
my brain isnt functioning well today at all
SinFluxx
SinFluxx12mo ago
And sometimes just take a break! Having a break then coming back to it can help
Aubrey
AubreyOP12mo ago
its because of the math exam i had today my math since then has been questionable
Want results from more Discord servers?
Add your server