[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 253

# Calculate cost of meal

I have a Visual C# application pictured here >> http://screencast.com/t/Tt3D9mqbfsCD that is supposed to calculate the cost of a multi-person meal at a restaurant.  The totals are supposed to accumulate until the program is exited and the resulting value after the last person has been entered should be shown in the overallTotalsTextBox.  The clear should reset the values so that a new meal can be entered but not erase the data stored in the totals field.

What is happening is that the totalTextBox is displaying the overallTotal.

How do I correct this mistake in my code below?
``````// Purpose: To calculate an Overall Total for the cost of a multi-person
// meal a restaurant.

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;

namespace _304_PennerT_MockExam1
{
public partial class mealCalculatorForm : Form
{
// Declare Constants
private decimal SALES_TAX = .07m;
private decimal TIP_RATE = .15m;

// Identify fields (same as a variable except at class level with lifetime of application running)
private decimal overallTotal = 0.0m;

public mealCalculatorForm()
{
InitializeComponent();
}
private void knifeforkPictureBox_Click(object sender, EventArgs e)
{
try
{
// Declare Varialbles
decimal charge;
decimal salesTax;
decimal tip;
decimal total;

// Convert Text Box Values (string values) to appropriate data
// type using a Parse method and assign to related variable.
charge = decimal.Parse(chargeTextBox.Text);

// Process Calculations
// This is often 4th section following constants, variables, and
// conversion of text box values to data type.
salesTax = charge * SALES_TAX;
tip = charge * TIP_RATE;
total = charge + salesTax + tip;

// Accumulate Total Charges - field declared at class level.
overallTotal += total;

// Display Numeric Output to appropriate control by converting
// the numeric value to a string value using the ToString method.
salesTaxTextBox.Text = salesTax.ToString("c");
tipTextBox.Text = tip.ToString("c");
totalTextBox.Text = overallTotal.ToString("c");
overallTotalTextBox.Text = overallTotal.ToString("c");
}
catch (Exception ex)
{
// Display the default error message and set focus.
MessageBox.Show(ex.Message);

// Set Focus
chargeTextBox.Focus();
}
}
private void clearButton_Click(object sender, EventArgs e)
{
// Clear values from all meals.
chargeTextBox.Clear();
salesTaxTextBox.Clear();
tipTextBox.Clear();
totalTextBox.Clear();
overallTotalTextBox.Clear();

// Set Focus
chargeTextBox.Focus();
}
private void exitButton_Click(object sender, EventArgs e)
{
// Exit form application
this.Close();

// Set Focus
chargeTextBox.Focus();
}
}
}
``````
0
rtod2
• 3
• 3
2 Solutions

Software EngineerCommented:
``````totalTextBox.Text = overallTotal.ToString("c");
``````

should be

``````totalTextBox.Text = total.ToString("c");
``````

Now you are writing the same overall total value to both Total and OverAll total textbox

Hope this helps
Raj
0

Software EngineerCommented:
>> The clear should reset the values so that a new meal can be entered but not erase the data stored in the totals field

If you don't want to erase Overall total value on Clear button click, you need to remove this code in Clear button click ?
`````` overallTotalTextBox.Clear();
``````

Raj
0

Commented:
You must be omitting something.

1. Is there a difference in 'totalTextBox' definition vs. the others?
2. Any other code that runs after ViewState?
3. Any client script?
0

Software EngineerCommented:
Raj
0

Commented:
in this part

private void clearButton_Click(object sender, EventArgs e)
{
// Clear values from all meals.
chargeTextBox.Clear();
salesTaxTextBox.Clear();
tipTextBox.Clear();
totalTextBox.Clear();
overallTotalTextBox.Clear();

// Set Focus
chargeTextBox.Focus();
}

you should remove the overallTextBox.Clear....  if you dont want it to clear out...

then on this part:

/ Display Numeric Output to appropriate control by converting
// the numeric value to a string value using the ToString method.
salesTaxTextBox.Text = salesTax.ToString("c");
tipTextBox.Text = tip.ToString("c");
totalTextBox.Text = overallTotal.ToString("c");
overallTotalTextBox.Text = overallTotal.ToString("c");
}

on this line

totalTextBox.Text = overallTotal.ToString("c");

totalTextBox.Text = total.ToString("c");
0

Commented:
You are incorrectly setting 'totalTextBox' text, as Raj pointed out.

However, I read your problem as saying that the 'totalTextBox' is displaying overall total after hitting the 'Clear' button.
0

Commented:
Clearing the overallTotal textbox won't clear the overallTotal member var, unless you are using some kind of data binding.
0

## Featured Post

• 3
• 3
Tackle projects and never again get stuck behind a technical roadblock.