Hey guys. I'm having some trouble with this program that I'm writing. At first I was getting an error saying 'Use of unassigned local variable 'dblAccumulatedCharges' and 'Use of unassigned local variable 'dblNumberOfGroups'. So I inserted 'dblAccumulatedCharges = 0;' and 'dblNumberOfGroups = 0;' into the code. From that point, I am able to run the program. My problem is that when I run the program, none of the output is correct, except the group name. The output for dblNumberOfGroups is stuck at zero, the output for dblAccumulatedCharges is stuck at zero, the average charge outputs 'NaN' (Not sure why it isn't recognized as a number), and the dblCurrentCharge seems to be incorrect. I would greatly appreciate any help, or for anyone just to point me in the right direction. I don't expect you to solve it for me. Pointing me in the right direction is a better learning process anyways. But, answer how you would like.
My .Net version is 3.5
Here is the code:
namespace Recording_Studio
{
public partial class Form1 : Form
{
//Global Declarations: Accumulated Charges, Total Amount of Groups, and a constant for the Rental Rate
double totalAccumulatedCharges = 0;
double totalNumberOfGroups = 0;
double rentalRate = 200 / 60;
double count = 0;
private void btnCalculate_Click(object sender, EventArgs e)
{
// Declarations: Input - Name, Minutes used.
// Output - Current charge, Group name, Accumulated Charges, Average Charge, Number of Groups
double dblMinutesUsed;
double dblAverageCharge, dblCurrentCharge, dblAccumulatedCharges, dblNumberOfGroups;
dblAccumulatedCharges = 0;
dblNumberOfGroups = 0;
//Input - Gather the data
try
{
dblMinutesUsed = double.Parse(txtMinutesUsed.Text);
}
catch
{
MessageBox.Show("Input was either non-numeric or blank", "Invalid Input!");
txtMinutesUsed.Focus();
return;
}
//Process - Calculate Current Charge, Accumulated Charges, Number of Groups, and Average charge
dblCurrentCharge = dblMinutesUsed * rentalRate;
dblAverageCharge = dblAccumulatedCharges / dblNumberOfGroups;
totalAccumulatedCharges = totalAccumulatedCharges + dblAccumulatedCharges;
totalNumberOfGroups = totalNumberOfGroups + dblNumberOfGroups;
//count++;
//totalAccumulatedCharges++;
//totalAmountOfGroups++;
//Output - Display the information
//Clear all the output listboxes
lstAccumulatedCharges.Items.Clear();
lstAverageCharge.Items.Clear();
lstCurrentCharge.Items.Clear();
lstGroupName.Items.Clear();
lstNumberOfGroups.Items.Clear();
//Display the information
lstAccumulatedCharges.Items.Add(dblAccumulatedCharges.ToString("n1"));
lstAverageCharge.Items.Add(dblAverageCharge.ToString("n1"));
lstCurrentCharge.Items.Add(dblCurrentCharge.ToString("n1"));
lstGroupName.Items.Add(txtGroupName.Text);
lstNumberOfGroups.Items.Add(dblNumberOfGroups.ToString("n1"));
}
private void btnClear_Click(object sender, EventArgs e)
{
// Clear all input text boxes and output list boxes
txtGroupName.Clear();
txtMinutesUsed.Clear();
lstAccumulatedCharges.Items.Clear();
lstAverageCharge.Items.Clear();
lstCurrentCharge.Items.Clear();
lstGroupName.Items.Clear();
lstNumberOfGroups.Items.Clear();
txtGroupName.Focus();
}
Alright. I've changed the declarations a bit and now when I run the program, it counts the number of times I have hit the calculate button aka total number of groups. The current charge also works correctly.
<code>
// Declarations: Input - Name, Minutes used.
// Output - Current charge, Group name, Accumulated Charges, Average Charge, Number of Groups
double dblMinutesUsed;
double dblAverageCharge, dblCurrentCharge, dblAccumulatedCharges, dblNumberOfGroups;
dblAccumulatedCharges = 0;
dblNumberOfGroups = 1;
</code>
The accumulated charges is still not working. Neither is the average charge. But instead of coming up with 'NaN', I get a zero. Which means it must be calculating "0 divided by 1". But I'm not sure how to go about coding it properly so that it doesn't occur.
Here is my new process:
<code>
//Process - Calculate Current Charge, Accumulated Charges, Number of Groups, and Average charge
dblCurrentCharge = dblMinutesUsed * rentalRate;
dblAverageCharge = totalAccumulatedCharges / dblNumberOfGroups;
dblAccumulatedCharges = dblCurrentCharge + totalAccumulatedCharges;
dblNumberOfGroups += totalNumberOfGroups;
totalNumberOfGroups++;
</code>
To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
If anyone felt he has got help, show it in rating the post.
Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ? My latest articles : Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7
// Declarations: Input - Name, Minutes used.
// Output - Current charge, Group name, Accumulated Charges, Average Charge, Number of Groups
double dblMinutesUsed;
double dblAverageCharge, dblCurrentCharge, dblAccumulatedCharges, dblNumberOfGroups;
dblAccumulatedCharges = 0;
dblNumberOfGroups = 1;
Code:
//Process - Calculate Current Charge, Accumulated Charges, Number of Groups, and Average charge
dblCurrentCharge = dblMinutesUsed * rentalRate;
dblAverageCharge = totalAccumulatedCharges / dblNumberOfGroups;
dblAccumulatedCharges = dblCurrentCharge + totalAccumulatedCharges;
dblNumberOfGroups += totalNumberOfGroups;
totalNumberOfGroups++;
how is dblAccumulatedCharges and totalAccumulatedCharges IMHO this both should be the same.
connected to each other. Because I cannot see any calculation where totalAccumulatedCharges will be other then zero.
Maybe at first clear up all your Fields and there definition and show the full method which influence them.
Last edited by JonnyPoet; November 5th, 2009 at 05:52 PM.
Jonny Poet
To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
If anyone felt he has got help, show it in rating the post.
Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ? My latest articles : Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7
Bookmarks