Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Debugging a logic error

Status
Not open for further replies.

khue

Programmer
Mar 6, 2001
112
US
I have the following codes:

<!--- Begin Read database --->
Assuming a successful read
<!--- End of read Database --->


<!--- If some courses are found, then add up all the course fees --->
<CFIF qryRegister_Course.recordcount GT 0>
<CFLOOP FROM=&quot;1&quot; TO=&quot;#qryRegister_Course.recordcount#&quot; INDEX=&quot;i&quot;>
<!--- Add all the course fee that belongs to this student --->
<CFSET Total_Course_Fee = Total_Course_Fee + qryRegister_Course.Course_fee>
</CFLOOP>
</CFIF>

...

<!--- Begin Read database --->
Assuming a successful read
<!--- End of read Database --->

<CFSET TotalBalance = 0.00>

<!--- Verify this student's total balance. If the accumulated courses fees does not equal to the total balance, then display error message. --->
<CFIF qryBalance.recordcount GT 0>
<CFSET TotalBalance = qryBalance.Total_Balance>

<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
window.alert(<CFOUTPUT>#TotalBalance#</CFOUTPUT> + &quot; NEQ &quot; + <CFOUTPUT>#Total_Course_Fee#</CFOUTPUT>)
window.alert(&quot;Error = &quot; + <CFOUTPUT>#ERROR#</CFOUTPUT>)
</SCRIPT>

<CFIF TotalBalance NEQ Total_Course_Fee>
<CFSET ERROR = 1>

<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
window.alert(&quot;Now ... Error = &quot; + <CFOUTPUT>#ERROR#</CFOUTPUT>)
</SCRIPT>

<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
// Recorded balance and calculated balance are not the same.
window.alert(&quot;There is a descrepency on the recorded balance for this student.&quot;)
</SCRIPT>
</CFIF>
</CFIF>


I have a bunch or records that this codes gets applied to but only one record when it gets to the compare statement:

<CFIF TotalBalance NEQ Total_Course_Fee>

it always falls through except the rest.

My data:

record #1: TotalBalance = 90.30 and Total_Course_Fee = 90.30
record #2: TotalBalance = 0.00 and Total_Course_Fee = 0.00
record #3: TotalBalance = 0.00 and Total_Course_Fee = 0.00
record #4: TotalBalance = 60.45 and Total_Course_Fee = 60.45
record #5: TotalBalance = 90.30 and Total_Course_Fee = 90.30

For Record #5, somehow, the condition is always true! I can figure out even when I try displaying the values of these two variables.

Would someone know what's wrong???
 
I don't know that I completely understand your problem but one thing I do see is that you're not setting the query attribute in your loop:


<CFLOOP FROM=&quot;1&quot; TO=&quot;#qryRegister_Course.recordcount#&quot; INDEX=&quot;i&quot;>
<!--- Add all the course fee that belongs to this student --->
<CFSET Total_Course_Fee = Total_Course_Fee + qryRegister_Course.Course_fee>
</CFLOOP>


This would cause some unpredictable results since it would total all your records using Course_fee from only one record. Andrew
 
What I'm saying is that, with the data that I have, I'm using this same code to read these data. I have two variables that I'm comparing. The first variable is the value of the accumulated course fees that a particular students is taking. The other variable is the value of the balance due for the same student. These two values should equal initially. With these 5 records, only one record, record 5, that meets this condition which it should not. I can't figure out why it is when the fact is that the two values are equal.

And &quot;Yes&quot;, I believe I do have the query attribute in my loop, unless what you are trying to tell is something else that I do not understand.

This is the actual code:

<!--- Initialize Total_Course_Fee Accumulator --->
<CFSET Total_Course_Fee = 0.00>

<!--- If some courses are found, then add up all the course fees --->
<CFIF qryRegister_Course.recordcount GT 0>
<CFLOOP FROM=&quot;1&quot; TO=&quot;#qryRegister_Course.recordcount#&quot; INDEX=&quot;i&quot;>
<!--- Add all the course fee that belongs to this student --->
<CFSET Total_Course_Fee = Total_Course_Fee + qryRegister_Course.Course_fee>

</CFLOOP>
</CFIF>

This code only gets executed when the user select a student. Therefore only one student's records gets accumulated and calculated. Not multiple students at the same time.

Inorder to read all 5 records, the user would need to select the five students that are related to one of these records in 5 separate times.
 
I believe you have to set the query attribute if you are trying to loop through a query using cfloop. Try this line instead of your current loop tag:

<CFLOOP QUERY=&quot;qryRegister_Course&quot;>

From my experience, by not specifying the query attribute it will just read the first record.
Andrew
 
Here is your code
..................................
<CFLOOP FROM=&quot;1&quot; TO=&quot;#qryRegister_Course.recordcount#&quot; INDEX=&quot;i&quot;>
<!--- Add all the course fee that belongs to this student --->
<CFSET Total_Course_Fee = Total_Course_Fee + qryRegister_Course.Course_fee>
</CFLOOP>
...................................

do like this in loop...

<CFSET Total_Course_Fee = Total_Course_Fee + qryRegister_Course[&quot;Course_fee&quot;]>

- Marc
 
Andrew,

So you are assuming that my result may have contained more than one records and this may have been the cause of the comparison to be true. I'm just trying to understand what you're telling.


Marc,

What effect does [&quot;Course_Fee&quot;] have on the accumulation variable Total_Course_Fee? I haven't seen this syntax before. Will you please explain a little bit?
 
Andrew and Marc, I've tried both of your solutions and the comparison still falls through.

I don't know what's wrong? Does ColdFusion have a function to convert a string numer into an integer/floating number?
 
Your query getting the details of a specific user, but your looping code doesn't seems to be getting all the records associated with that query, because you are omitting the query name. Your cfloop statement looping as many time as the record count, but it always getting the static(same) value for qryRegister_Course.Course_fee.

Try this one


<CFLOOP query=&quot;qryRegister_Course&quot;><!-- dont put ##--->
<!--- Add all the course fee that belongs to this student --->
<CFSET Total_Course_Fee = Total_Course_Fee + Course_fee>
</CFLOOP>
 
ram123,

I've tested your solution and the condition (comparison) still falls through. I don't know. I don't understand why the first record which has the same values, did not have this problem.
 
So if you output the values for Totalcoursefee, and total balance variables what values you are getting for each record?
 
ram123,

When I output/displays the values of Total_Course_Fee and TotalBalance they are the same, i.e. Total_Course_Fee = TotalBalance.
 
It is strange. Comment out your existing comparision code blocTime being) and try to execute the following code with all records. If you still get the same problem you may need to use functions like trim and or evaluate.
<CFIF TotalBalance NEQ Total_Course_Fee>
Both are not equal <br>
Total Balance is : #TotalBalance# <br>
Total course fee is #Total_course_fee# <br>
length of total balance field is #len(TotalBalance)#<br>
length of Total Course fee field is #len(Total_Course_Fee)#
<cfelse>
Exactly equal <br>
Total Balance is : #TotalBalance# <br>
Total course fee is #Total_course_fee# <br>
length of total balance field is #len(TotalBalance)#<br>
length of Total Course fee field is #len(Total_Course_Fee)#
</cfif>
 
ram123,

I have inserted your code and commented out my comparison code. This is the result:

Both are not equal
Total Balance is : 90.3000
Total course fee is 90.3
length of total balance field is 7
length of Total Course fee field is 4

I believe that the trailing zeroes should not effect the comparison; or does it?

If so, how do I trim these trailing zeroes?
 
ram123,

Thanks for all your help. When I used the Evaluate() function, that eliminate leading spaces/zeroes and trailing spaces/zeroes. Thank you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top