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.
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!
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.
Re: Problem with my code...
<code>
</code>
This are wrong codetags we use square brackets !
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.