Thread: why is math not calculating properly

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    47

    Lightbulb why is math not calculating properly

    In my code, I cannot figure why it still is calculating wrong:

    here is the execute:

    BANK RECONCILIATION FOR: 10/21/98

    Ending balance on statement:350
    Enter Outstanding dep (or 0 to quit)>75
    Enter outstanding dep (0 to quit)>20
    Enter outstanding dep (0 to quit)>150
    Enter outstanding dep (0 to quit)>25
    Enter outstanding dep (0 to quit)>0
    Outstanding withdrawal (or 0 to quit)>57.85
    Enter withdrawal (0 to quit)>200
    Enter withdrawal (0 to quit)>50
    Enter withdrawal (0 to quit)>125.38
    Enter withdrawal (0 to quit)>0
    Your final balance in your checkbook should be $ 169.62

    Why is this happening it should be: 186.77

    Code:
    #include <stdio.h>
    
    #define SENTINEL 0
    
    float finbal, outdep, withdraw;
    
    int main (void)
    
    
    {
    
    		  printf("BANK RECONCILIATION FOR: 10/21/98");
    
    
    	      printf("\n\nEnding balance on statement:");
    		  scanf ("%f", &finbal);
    
    
    
    		  printf("Enter Outstanding dep (or %d to quit)>", SENTINEL);
    		  scanf ("%f", &outdep);
    
    while (outdep!= SENTINEL)
    	{
    
    		printf("Enter outstanding dep (%d to quit)>", SENTINEL);
    		scanf("%f", &outdep);
    		finbal = finbal + outdep;
    
    	}
    
    printf("Outstanding withdrawal (or %d to quit)>", SENTINEL);
    scanf ("%f", &withdraw);
    
    while (withdraw!=SENTINEL)
    	{
    
    		printf("Enter withdrawal (%d to quit)>", SENTINEL);
    		scanf("%f", &withdraw);
    	    finbal = finbal - withdraw;
    	}
        /* the outstanding deposits and withdrawals, are already sub-totaled
        into finbal. So this is all you need:
        */
    
    
    	printf("Your final balance in your checkbook should be $ %.2f\n" , finbal);
    
    	return 0;
    
      }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Do you have any plans to add the first deposit and subtract the first withdrawal?

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    47
    I do not understand.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The first deposit. You don't add it in, you go straight to asking for the second deposit.

    The first withdrawal. You don't subtract it out, you go straight to asking for the second withdrawal.

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    47
    I see what you mean but I do not know how to fix this

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Order the three statements in your loop in the correct order (i.e., the first thing you must do is process the validated transaction, then print a prompt, then get input).

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by kburyanek View Post
    I see what you mean but I do not know how to fix this
    As long as you understand what each line does then this level of programming is barely any more complex than writing a simple recipe for baking a cake. If the recipe you create says to add dry ingreedients, ice the cake, allow it to cool, place it in oven, cook 40 minutes, and then add butter and egg, well you've gotten a few things horribly wrong. If however you can sort that kind of thing out, then you can figure out how to solve your problem, just give yourself some time to think about it.

    If it really wont come to you then you're going to need to either get a good personal tutor, or reconsider why you're attempting to learn to program. Programming is not for everyone.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I posted a working version of his program in his other thread on this topic.

    It doesn't need any more changes. The logic is "odd", but it works fine, within the accuracy of a float that isn't "normalized".

    This version calculates wrong because he made the changes manually, and goofed.

    Copy and paste - I double checked the corrected version that I posted up, and it's fine.
    Last edited by Adak; 10-15-2009 at 01:08 AM.

  9. #9
    Registered User
    Join Date
    Sep 2009
    Posts
    47
    Adak,

    I changed it back to my original, It does work. Thank you. I made other changes that were commented on im the thread and that threw me way off.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Adak View Post
    I posted a working version of his program in his other thread on this topic.

    It doesn't need any more changes. The logic is "odd", but it works fine, within the accuracy of a float that isn't "normalized".

    This version calculates wrong because he made the changes manually, and goofed.

    Copy and paste - I double checked the corrected version that I posted up, and it's fine.
    That's because you made the fix we suggested and he didn't. (Or more to the point, he randomly decided to change that and yet didn't realize that could be the problem.)
    Last edited by tabstop; 10-15-2009 at 07:48 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with making a Math Expression DLL
    By MindWorX in forum C Programming
    Replies: 19
    Last Post: 07-19-2007, 11:37 PM
  2. how to use operator+() in this code?
    By barlas in forum C++ Programming
    Replies: 10
    Last Post: 07-09-2005, 07:22 PM
  3. Recursion
    By Lionmane in forum C Programming
    Replies: 11
    Last Post: 06-04-2005, 12:00 AM
  4. Help with C++ Math
    By aonic in forum C++ Programming
    Replies: 4
    Last Post: 01-29-2005, 04:40 AM
  5. toughest math course
    By axon in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 10-28-2003, 10:06 PM