Thread: calculation problem

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

    calculation problem

    I cannot figure out why this program will not calculate the final balance. I enter the end bal and the outdep and the with but in the final balance, keeps coming up with the same amount entered as the end bal.

    Any help would be appreciated.

    Thanks
    Code:
    #include <stdio.h>
    
    #define SENTINEL 0
    
    float endbal,
    	  finbal;
    int	  date,
    	  outdep,
    	  withdraw;
    
    int main (void)
    
    
    {
    
    		  printf("BANK RECONCILIATION FOR: 10/21/98");
    
    		  printf("\n\nEnding balance on statement:");
    		  scanf ("%f", & endbal);
    
    
    		  printf("Enter Outstanding dep (or %d to quit)>", SENTINEL);
    		  scanf ("%f", & outdep);
    
    while (outdep!= SENTINEL)
    	{
    		finbal = finbal + outdep;
    		printf("Enter outstanding dep (%d to quit)>", SENTINEL);
    		scanf("%f", & outdep);
    
    	}
    
    printf("Outstanding check/withdrawal (or %d to quit)>", SENTINEL);
    scanf ("%d", & withdraw);
    
    while (withdraw!=SENTINEL)
    	{
    		finbal = finbal - withdraw;
    		printf("Enter withdrawal (%d to quit)>", SENTINEL);
    		scanf("%f", & withdraw);
    
    	}
    
        finbal = endbal + outdep - withdraw;
    
    	printf("Your final balance in your checkbook should be $ %f\n" , finbal);
    
    	return 0;
    
      }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Let me put it into the "toaster".

    You're confusing your data types. Outdep and withdraw are int's, but you're scanf() is taking them in as floats.

    You need to use either int's or floats for these variables.
    Last edited by Adak; 10-14-2009 at 08:42 AM.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    19
    Code:
         finbal = endbal + outdep - withdraw;
    By the time that you get to this line of code endbal hasn't changed, outdep = 0 and withdraw = 0.

  4. #4
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    In the end you do:
    Code:
    finbal = endbal + outdep - withdraw
    So finbal = endbal
    outdep = withdraw = SENTINEL = 0 since you exit from the loops

    Maybe you wanted just:
    Code:
    finbal = finbal  + endbal
    Also, you have unecessary code
    Code:
    		  
    printf("Enter Outstanding dep (or %d to quit)>", SENTINEL);
    scanf ("%f", & outdep);
    while (outdep!= SENTINEL)
    	{
    		finbal = finbal + outdep;
    		printf("Enter outstanding dep (%d to quit)>", SENTINEL);
    		scanf("%f", & outdep);
    
    	}
    Can be done like:
    Code:
    do	{
    		finbal = finbal + outdep; //outdep is 0 in the beginning
    		printf("Enter outstanding dep (%d to quit)>", SENTINEL);
    		scanf("%f", & outdep);                
    	} while (outdep!= SENTINEL);

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This works, within the limitations of the accuracy of floats, which can't represent all dollars and cents, accurately.

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

    1) "normalize" the errors out of the float values

    or

    2) throw away the float data types, and use int's for dollars, and int's for cents.

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    47
    I changed and then did an execute and this is what my answer was. Still wrong answer for finbal. Thanks for all of your advice on the above changes.



    BANK RECONCILIATION FOR: 10/21/98

    Ending balance on statement:10
    Enter outstanding dep (0 to quit)>10
    Enter outstanding dep (0 to quit)>10
    Enter outstanding dep (0 to quit)>10
    Enter outstanding dep (0 to quit)>0
    Enter outstanding dep (0 to quit)>5
    Outstanding check/withdrawal (or 0 to quit)>5
    Enter withdrawal (0 to quit)>5
    Enter withdrawal (0 to quit)>0
    Your final balance in your checkbook should be $ 2193620992.000000

    "c:\cop2000\a5\lcc2\score.exe"
    Return code 0
    Execution time 9.094 seconds
    Press any key to continue...

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by kburyanek View Post
    I changed and then did an execute and this is what my answer was. Still wrong answer for finbal. Thanks for all of your advice on the above changes.



    BANK RECONCILIATION FOR: 10/21/98

    Ending balance on statement:10
    Enter outstanding dep (0 to quit)>10
    Enter outstanding dep (0 to quit)>10
    Enter outstanding dep (0 to quit)>10
    Enter outstanding dep (0 to quit)>0
    Enter outstanding dep (0 to quit)>5
    Outstanding check/withdrawal (or 0 to quit)>5
    Enter withdrawal (0 to quit)>5
    Enter withdrawal (0 to quit)>0
    Your final balance in your checkbook should be $ 2193620992.000000

    "c:\cop2000\a5\lcc2\score.exe"
    Return code 0
    Execution time 9.094 seconds
    Press any key to continue...
    There may be a copy fail, I'll check. My version runs correctly with the above input.

    In red, is the obvious error. You've exited the loop, by entering zero, but it's still asking for your outstanding dep. << holy zombie loops, batmat! >>

    I'll check out the pasted copy of my version, and let you know, in just a bit.

    Edit:

    You need to copy and paste, not change it. You changed it wrong, and that's why it didn't work. Copy and paste, and it works.
    Last edited by Adak; 10-14-2009 at 05:50 PM.

  8. #8
    Registered User
    Join Date
    Sep 2009
    Posts
    47
    I changed it back to the way you stated that I should have, I was listening to the other person that was trying to tell me to shorten some of the code. Then I really messed up. Thank you for your help with this Adak.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You're welcome and I'm glad it's working.

    Please keep just *one* thread per subject on the forum, in the future. Bump up the thead, if it needs it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  3. Calculation problem....
    By badran in forum C++ Programming
    Replies: 6
    Last Post: 10-17-2006, 04:07 AM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  5. Trigonometry Sin rule calculation problem
    By Harry in forum C++ Programming
    Replies: 3
    Last Post: 02-06-2006, 12:14 PM