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

Thread: Problem with my code...

  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

  2. #2
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    210

    Re: Problem with my code...

    please use code-tags in future to make code better readable.


    "dblNumberOfGroups is stuck at zero"
    Well, I see no piece of code, where you change this value other than 0.

    "dblAccumulatedCharges is stuck at zero"
    Also here I see no piece of code, where you change this value other than 0.


    "the average charge outputs 'NaN'"...
    look at this: dblAverageCharge = dblAccumulatedCharges / dblNumberOfGroups;

    Ever tried to divide a number by zero? This is a math ERROR by default.
    dblNumberOfGroups = 0 and will have no other value than 0.
    Last edited by MNovy; November 5th, 2009 at 01:21 AM.

  3. #3
    Join Date
    Oct 2009
    Posts
    6

    Re: Problem with my code...

    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>

    Thank you for any help!

  4. #4
    Join Date
    Oct 2009
    Posts
    6

    Re: Problem with my code...

    I'm sure that my problem is due to variable scope. And I'm trying to take the average too soon.

  5. #5
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,537

    Re: Problem with my code...

    <code>
    </code>

    This are wrong codetags we use square brackets !
    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

  6. #6
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,537

    Re: Problem with my code...

    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:
    //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 06: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

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center