Thread: I can't figure this out!

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    33

    Question I can't figure this out!

    I have this whole program working except for the following:

    If the current balance goes to zero, then no more withdrawals should be made and it should output a message " Sorry you have no money to withdraw"

    This is the only part I can't figure out how to do. I have been trying for days now. Can someone help? Code is below



    Code:
    #include <stdio.h>
    
    int main ()
    
    {
    	 /*Variable Declarations*/
    	 /*---------------------*/
    
    	 int   x, withdrawals, deposits;
    	 float deposit[50], withdrawal[50];
    	 float total=0, balance;
    
    
    	 /*Declare and initalize accumulators for closing balance*/
    	 /*------------------------------------------------------*/
    
    	 float sum= 0, count=0;
    
    	 /*Display opening greeting to user*/
    	 /*--------------------------------*/
    
    	     printf ("\nWelcome to Colleen's Banking System\n\n");
    
    	 /*Prompt user to input current balance in dollars and cents*/
    	 /*---------------------------------------------------------*/
    
    	  do
    	  {
    		  printf ("Enter current balance in dollars and cents: "  );
    		  scanf  ("%f", &balance);
    		  fflush (stdin);						//clear input screen
    
    		  if (balance < 0 )
    		  printf ("Beginning balance must be at least zero, please re-enter!\n\n");
    	 } while (balance < 0 );            //end do loop
    
    	 /*Prompt user to input number of withdrawals*/
    	 /*------------------------------------------*/
    
    		do
    		{
    		  printf ("\nEnter the number of withdrawals: " );
    		  scanf  ("%d", &withdrawals);
    		  fflush (stdin);                 //clear input screen
    
    		  if (withdrawals < 0 )
    		  printf ("Error: Number of withdrawals must be at least zero, please re-enter! \n");
    	 } while (withdrawals < 0 );         //end do loop
    
    	 /*Prompt user to input the number of deposits*/
    	 /*-------------------------------------------*/
    
    		do
    		{
    		  printf ("\nEnter the number of deposits: " );
    		  scanf  ("%d", &deposits);
    		  fflush (stdin);                 //clear input screen
    
    		  if (deposits < 0 )
    		  printf ("Error: Number of deposits must be at least zero, please re-enter! \n");
    	 } while (deposits < 0 );            //end do loop
    
    	 /*Promt user to input each deposit amount*/
    	 /*---------------------------------------*/
    
    	 for( x = 1; x <= deposits; x++)
    	 {
    		  printf ("Enter the amount of deposit #%i: ", x);
    		  scanf  ("%f", &deposit[x]);
    		  fflush (stdin);                  //clear input screen
    
    	 /*Output appropriate message if deposit amount is less than zero.*/
    	 /*---------------------------------------------------------------*/
    
    	 if ( deposit < 0 )
    		{
    		  printf ("***Invalid entry. Deposit amount must be positive.***\n");
    		  deposit[x] = deposit - deposit;
    		  x--;
    		}
    
    		count = count + deposit[x];
    
    	 } // end for loop
    
    	 /*Prompt user to input each withdrawal amount*/
    	 /*----------------------------------------------------*/
    
    	 for( x = 1; x <= withdrawals; x++)
    		{
    		  printf ("Enter the amount of withdrawal #%i: ", x);
    		  scanf  ("%f", &withdrawal[x]);
            fflush (stdin);                  //clear input screen
    
    	 /*Output appropriate message if withdrawal amount is not positive.*/
    	 /*----------------------------------------------------------------*/
    
    	 if ( withdrawal < 0 )
    		{
    		  printf ("***Withdrawal amount must be positive.***\n");
    		  withdrawal[x] = withdrawal - withdrawal;
    		  x--;
    		}
    
    		/*Output appropriate message if withdrawal exceeds current balance.*/
    		/*-----------------------------------------------------------------*/
    
    	 if (withdrawal[x] > balance + deposit[x])
    		{
    		  printf ("***Withdrawal amount exceeds current balance. Please re-enter!***\n");
    		  withdrawal[x] = withdrawal - withdrawal;
    		  x--;
    		}
    
    	 sum = sum + withdrawal[x];
    
    	  /*If balance goes to zero, no more withdrawals should be made*/
    	  /*-----------------------------------------------------------*/
    
    	 if (withdrawal[x] = balance + deposit[x])
    	{
    		  printf ("***Sorry you have no money to withdraw!***\n");
    		  withdrawal[x] = withdrawal - withdrawal;
    	}
    
    	 } // end for loop
    
    	 /*Output closing balance amount to user*/
    	 /*-------------------------------------*/
    
    	 total = (float) balance + count - sum;
    	 printf ("\n***The closing balance is $%.2f***\n", total);
    
    	 /*Display short advice for user based upon closing balance*/
    	 /*--------------------------------------------------------*/
    
    	 if (total >= 500000.00 )
    		  printf ("***Time to invest some money!*** \n");
    	 else
    	 if (total > 15000.00&&total <49999.99 )
    		  printf ("***Maybe you should consider a CD.*** \n");
    	 else
    	 if (total > 1000.00&&total <14999.99 )
    		  printf ("***Keep up the good work.*** \n");
    	 else
    	 if (total >=0.00&&total< 999.99 )
    		  printf ("***Your balance is getting low.***\n");
    
    	 /*Output a Bank Record for user*/
    	 /*-----------------------------*/
    
    		  printf ("\n***Bank Record***");
    		  printf ("\nStarting Balance: $ %.2f\n\n", balance);
    
    	 for (x=1; x <= deposits; x++)
    	 {
    		  printf ("Deposit #%i:        %.2f\n", x, deposit[x]);
    	 } //end for loop
    
    	 for (x=1;  x <= withdrawals; x++)
    	 {
    		  printf ("\nWithdrawal #%i:     %.2f", x, withdrawal[x]);
    	 } //end for loop
    
    		  printf ("\n\nEnding Balance: $   %.2f\n", total);
    
    
    } //end main
    Last edited by paulntysmom; 04-05-2006 at 02:12 PM.

  2. #2
    Registered User wintellect's Avatar
    Join Date
    Mar 2006
    Posts
    24
    the only test against balance I see is here:
    Code:
    if (withdrawal[x] > balance + deposit[x])
    	{
    		  printf ("***Withdrawal amount exceeds current balance. Please re-enter!***\n");
    		  withdrawal[x] = withdrawal - withdrawal;
    		  x--;
    	}
    This is where your error message should appear and code to stop any further requests. Or have I failed to understand your program? I see no other place where balance is tested.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    33
    I want it that if the withdrawal amount exceeds the current balance (including new deposits) it should output the following " Withdrawal amount exceeds current balance, it should then reprompt the user for a lower withdrawal amount (which it does this)

    and also That if the current balance goes to zero, no more withdrawals should be made and a appropriate message should display. (this is the part I am having problems with)

    Another small question and I don't even know if this is possible. Is their any way to get the decimal points on the bank record to line up. I been trying to but haven't been sucessful.



    I have updated the code in my first post.
    Last edited by paulntysmom; 04-05-2006 at 02:13 PM.

  4. #4
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Comparing the two floats:
    Code:
    if (withdrawal[x] = balance + deposit[x])
    you should probably use the comparison ==, not the assignment =.

    Also, comparing floats for exact equality is rarely going to work because floats are not exact. 23.50 could in fact be 23.500001 or 23.4999997. Even though they are close, they are not equal. Your best bet is to use => or =<
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    33
    I actually updated it to this before even seeing your post, after reading more in the four books I have. Trying to self teach myself.
    you should probably use the comparison ==, not the assignment =.


    Confused on this part below. What am I changing in the program for this?

    Also, comparing floats for exact equality is rarely going to work because floats are not exact. 23.50 could in fact be 23.500001 or 23.4999997. Even though they are close, they are not equal. Your best bet is to use => or =<
    Last edited by paulntysmom; 04-05-2006 at 07:56 PM.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    fflush(stdin);
    Read this. There are usually numerous posts about this on the board... I'd suggest a quick search.

    As for the comparison, for the purposes of dealing with this problem, many banks I believe always store data as cents and not dollars. Therefore all computations done on cents don't have to worry about round off problems or inaccuracies with floating point formats. If you are commited to using floats, then comparing such a float to another value using == might not work depending on the particular values involved. One way to deal with this is to use >= and <= to say whether or not the value you are checking is within a certain range of the value you are checking against. There is usually a defined constant in a header somewhere (<float.h> perhaps?) called FLT_EPSILON that represents the smallest possible float value (there is a cooresponding value called DBL_EPSILON for doubles). You can use this defined constant in the <= >= checks:

    Code:
    float value;
    printf("Enter a value: ");
    scanf("%f",&value);
    // Test if entered value is equal to 23.5... beware of using == directly
    if( value <= 23.5f + FLT_EPSILON && value >= 23.5f - FLT_EPSILON )
    {
        // It is "equal"/"close enough"
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Registered User wintellect's Avatar
    Join Date
    Mar 2006
    Posts
    24
    Quote Originally Posted by paulntysmom
    and also That if the current balance goes to zero, no more withdrawals should be made and a appropriate message should display. (this is the part I am having problems with)
    I see nowhere in your code where balance will get to 0

    You never change the value of balance - so how can it reach zero?

  8. #8
    Registered User
    Join Date
    Feb 2006
    Posts
    33
    Ok from everyones help. I should be writing it like this?
    Code:
     /*If balance goes to zero, no more withdrawals should be made*/
     /*--------------------------------------------------------------------------------*/
    
    	 if (balance = 0) 
    	{
    		  printf ("***Sorry you have no money to withdraw!***\n");
    		
    	}
    I am confused on what I should put below the printf and if the "if" command is the right choice. Also where do I put it in the code so it will execute correctly??

    Thanks again for everyones help. This board has taught me so much and I love coming on here everyday and reading the posts. I learned the most from just reading everyone's questions and the solutions

  9. #9
    Registered User wintellect's Avatar
    Join Date
    Mar 2006
    Posts
    24
    Think of it like this, balance has to be reduced by an amount (probably the withdrawl amount). Then you can test if balance is 0 or less and take appropriate action.

    Yes, an if() statement is fine, but you should be using the comparison "==" not the assignment "="

    Also, I'll leave it up to you to find out where it should go. YOU should understand how your code works and were it needs to be tested

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Figure out how method was called?
    By jcafaro10 in forum C++ Programming
    Replies: 2
    Last Post: 02-07-2009, 10:43 AM
  2. 3 dimensional figure volume
    By thekautz in forum C++ Programming
    Replies: 2
    Last Post: 01-20-2009, 05:22 PM
  3. trying to figure out someone's code for a plugin
    By paulpars in forum C++ Programming
    Replies: 4
    Last Post: 07-20-2006, 10:57 AM
  4. newb to C, cant figure something out.
    By Phate4219 in forum C Programming
    Replies: 16
    Last Post: 03-06-2006, 01:47 AM
  5. ahh i can't figure out this loop
    By blindleaf in forum C Programming
    Replies: 1
    Last Post: 03-18-2003, 09:42 AM