Hi
I try to write this program but how I think math?

A general store has the following offer.
If you shop for a minimum of 200 \$ will receive a 5% discount on the full amount. If you shop for at least
400 \$, you get 10% off the full amount
Write a program that calculates how much you will pay based on the full amount of the

If the total amount is greater than \$399, multiply it by .9 to get the total after a 10% discount. Otherwise, if the amount is greater than \$199, multiply it by .95 for a 5% discount. Otherwise, don't make any changes:

``````// Apply discounts
if (total > …``````

What about last part"how much you will pay based on the full amount of the items you want to buy." ?

It just means that the total amount prior to discount is what dictates the discount.

You might want to consider a maskedtextbox:

``````using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Globalization;

namespace TestCode2
{
public partial class Form2 : Form
{

public Form2()
{
InitializeComponent();

## All 10 Replies

If the total amount is greater than \$399, multiply it by .9 to get the total after a 10% discount. Otherwise, if the amount is greater than \$199, multiply it by .95 for a 5% discount. Otherwise, don't make any changes:

``````// Apply discounts
if (total > 399)
total *= .9;
else if (total > 199)
total *= .95;
``````

Thank you very much for quick answer.
What about last part"how much you will pay based on the full amount of the items you want to buy." ?

What about last part"how much you will pay based on the full amount of the items you want to buy." ?

It just means that the total amount prior to discount is what dictates the discount.

Thanks Sir
Do you know why I got error?

``````Why I got error?

decimal total;
decimal result;
bool buff;
buff = double.TryParse(txtTotal.Text, out total);
if (buff == false)
{
MessageBox.Show("Input Error");
txtTotal.Focus();
return;
}

//Disply result
result = 0;
if (total > 599)
{
total *= .9;

}
else if (total > 299)
{
total *= .95;
}
txtDiscount.Text = result.ToString();
``````

What did the error say?

Try putting `buff = double.TryParse(txtTotal.Text, out total);` in a try catch like this to see if it works:

``````try
{
double.TryParse(txtTotal.Text, out total);
buff = true;
}
catch
{
buff = false;
}
``````

Also you return result but result is set to 0 and never changed so you will always return 0.

I just get "Input Error" from the MessageBox.Show
I thing my code is wrong!

You're trying to parse a double into a decimal variable. Change either the definition of the decimal to double or change the parse of double to decimal.

Either that, or the text value you're passing can't be parsed as a number.

e.g ten dollars is nonesense to that method and will return false. Even \$10 will fail because it includes a non-numeric character.

Yep it is definetly the Parse that s causing the problem. Your variable buff always returns false and doesn t go on with the code. one more thing. after setting the result variable to 0 you are not using it anymore so just write at the end instead of result.ToString() --txtDiscount.Text = total.ToString();

You might want to consider a maskedtextbox:

``````using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Globalization;

namespace TestCode2
{
public partial class Form2 : Form
{

public Form2()
{
InitializeComponent();
}
{
}

private void button1_Click(object sender, EventArgs e)
{
double total;
//Disply result
if (total >=400)
{
total *= .9;
}
else if (total >= 200)
{
total *= .95;
}
txtDiscount.Text = total.ToString("####.##",CultureInfo.InvariantCulture);
}
}
}
``````

Any character entered that isn't a digit or if the decimal is put in the wrong place the value will clear. But this can be handled any number of different ways. The only obvious caveat with this, is all 6 six digits have to entered, which means you will need to enter leading zeros. The number of digits can also be easily changed.

Thank you very much Sir.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.