Question remainder calculation

clone99

New member
Joined
Oct 17, 2022
Messages
3
Programming Experience
1-3
Having problems with the following code in windosappalication(C#). The code works perfectly in the console application.

The problem is that if I I enter

Price 40;

Pay = 230;

Print it out

1 fifty dollars

1 dollar



C#:
int price = int.Parse(tbxprice.Text); // TEXTBOX 1

int pay = int.Parse(tbxbet.Text); // text box2

int rest = pay - price;

int remainder = rest;

int fivehundred = remainder / 500;

remainder = remainder % 500;

int twohundred = remainder / 200;

remainder = remainder % 200;

int hundred = remainder / 100;

remainder = remainder % 100;

int ffemtio = remainder / 50;

remainder = remainder % 50;

int twenty = remainder / 20;

remainder = remainder % 20;

int ten = remainder / 10;

remainder = remainder % 10;

int five = remainder / 5;

remainder = remainder % 5;

int one = rest; // SEK 1

if (price > pay)

{

  lblresponse.Text = " error ";

} else {

  if (fivehundred != 0)

  {

    lblsvar.Text = fivehundred +

  }

  if (twohundred != 0)

  {

    lblsvar.Text = two hundred + "st two hundred dollar \n";

  }

  if (hundred != 0)

  {

    lblsvar.Text = hundred + "st hundred dollar \n";

  }

  if (ffemtio != 0)

  {

    lblsvar.Text = ffemtio + "st fifty dollar \n ";

  }

  if (twenty != 0)

  {

    lblsvar.Text = twenty + "st twenty dollar \n";

  }

  if (ten != 0)

  {

    lblsvar.Text = ten + "st ten dollar \n ";

  }

  if (five != 0)

  {

    lblsvar.Text = five + "st five dollar \n";

  }

  if (one != 0)

  {

    lblsvar.Text = one + " 1st dollar \n";

  } else

  {

    lblanswer.Text = "";

  }

}
 
Last edited by a moderator:
You need to debug your code. You don't just run it and see what the result is. You set a breakpoint at the top of the code and step through it line by line, examining the state at each step. You need to determine what you expect to happen before each step and what actually happened after each step. When reality doesn't meet expectation, you have found an issue. If you don't know how to fix it, at least you can explain to us everything you learned from debugging.
 
Also, I doubt that you are reporting the output correctly. The result should have been "190 1st dollar" based on the code you presented.

Recall that setting the text property of a control does not append to the text displayed by the control. It replaces the existing text in the control.
 
Posted code does not compile due, at least, to line lblsvar.Text = fivehundred +

(And now to explain some of the terms in jmc's post..)

Click on a line of code at the top of your program; it has to be a line that does something, like the int price=... one

Press F9; the line goes red

Press play at the top of the visual studio window. Make sure the dropdown next to it says Debug

Click whatever buttons etc needed to get this calculation to run. The code stops with a yellow bar. The bar shows the line of code that has not been run yet. Press F10 to run the highlighted line of code and move on one step

At any step you can point to the variables you see to check their value. You can also look in the locals or autos window (show it via the Debug menu..Windows item) to see variable values and expand objects to see their property values

Use this process of going step by step to check that the values you expect are being calculated correctly. Find where they are not and fix the problem

-

Side note, your code should contain way more comments than it does. You should write out the algorithm in plain English and then write c# underneath it. You don't think in c#, you think in English (or whatever native language). Write the process in your native language and then translate it. This will help you see where you've gone wrong; if a comment says "increment tracker variable to process the next item" and the tracker variable decreased instead, there's a bug; a difference between something expected for the algorithm to work, and the reality of what is coded
 
Back
Top Bottom