CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Threaded View

  1. #1
    Join Date
    Oct 2009
    Posts
    6

    Problem with my code...

    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;

    public Form1()
    {
    InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    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();
    }

    private void btnExit_Click(object sender, EventArgs e)
    {
    this.Close();
    }
    }
    }


    Thank you very much, your help is greatly appreciated!
    Last edited by CodeLearner90; November 4th, 2009 at 08:33 PM. Reason: Failed to provide .net version

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured