Thread: What am I missing?

  1. #1
    Registered User
    Join Date
    Feb 2018
    Posts
    1

    What am I missing?

    I am having an issue. When I run this, it misses going to the beg_balance and goes directly to asking for the nm_deposits. Then, it wont take numbers and I am not sure why

    Code:
    #include<stdio.h>
    
    
    
    
    main ()
    {
    //Declare variables
      int i, s;
      int nm_deposits;
      int nm_withdrawals;
      float beg_balance;
      float end_balance;
      float deposit_ttl = 0;
      float deposits[5];
      float withdrawals[5];
    
    
    //Begin taking information
      int x = 1, y = 1;
    
    
      
      char c;
    
    
      printf("Welcome to the Computer Banking System\n\n");
    
    
      while (beg_balance <= -1)
        {
          printf ("Enter your current balance in dollars and cents:");
          scanf ("%f", &beg_balance);
    
    
          if (beg_balance <= -1)
    	{
    	  printf
    	    ("*** Beginning balance must be at least zero, please re-enter. \n\n");
    
    
    	  continue;
    	}
          end_balance = end_balance + beg_balance;
        }
    
    
      //Get Number of Deposits
      do
        {
          printf ("\nEnter the number of deposits (0 - 5):");
          scanf ("%i", &nm_deposits);
          while ((c = getchar () != '\n') && c != EOF);
          //Error Check
          if (deposits < 0 || deposits > 5)
    	printf ("*** Invalid number of deposits, please re-enter.\n");
        }
      while (deposits < 0 || deposits > 5);
    
    
      do
        {
          //Get Number of Withdrawals
          printf ("\nEnter the number of withdrawals (0 - 5):");
          scanf ("%i", &nm_withdrawals);
          while ((c = getchar () != '\n') && c != EOF);
          //Error Check
          if (nm_withdrawals < 0 || nm_withdrawals > 5)
    	printf ("*** Invalid number of withdrawals, please re-enter.\n");
          printf ("\n");
        }
      while (nm_withdrawals < 0 || nm_withdrawals > 5);
      //Get Deposit Amounts
      while (x <= nm_deposits)
        {
          printf ("Enter the amount of deposit #%i:", x);
          scanf ("%f", &deposits[x]);
          //Error Check
          if (deposits[x] < 0)
    	{
    	  printf
    	    ("*** Deposit amount must be greater than zero, please re-enter.\n\n");
    	}
          else if (deposits[x] >= 0)
    	{
    	  end_balance = end_balance + deposits[x];
    	  x++;
    	  while ((c = getchar () != '\n') && c != EOF);
    	}
        }
    
    
      printf ("\n");
      //Get Withdrawal Amounts
      while (y <= withdrawals)
        {
          printf ("Enter the amount of Withdrawal #%i:", y);
          scanf ("%f", withdrawals[y]);
    
    
          if (withdrawals[y] > end_balance)
      
    	{
    	  printf ("*** Withdrawal amount exceeds current balance, please re-enter.\n\n");
    	}
          else if (withdrawals[y] <= end_balance)
    	{
    	  end_balance = end_balance - withdrawals[y];
    	  y++;
    	  while ((c = getchar () != '\n') && c != EOF);
    	}
        }
    
    
      printf ("\n*** The closing balance is $%.2f ***\n", end_balance);
    
    
      if (end_balance >= 50000.00)
        printf ("*** It is time to invest some money! ***");
    
    
      else if (end_balance >= 15000.00 && end_balance <= 49999.99)
        printf ("*** Maybe you should consider a CD. ***");
    
    
      else if (end_balance >= 1000.00 && end_balance <= 14999.99)
        printf ("*** Keep up the good work! ***");
    
    
      else if (end_balance >= 0.00 && end_balance <= 999.99)
        printf ("*** Your balance is getting low! ***");
      //Final Calulations and financial advice
      end_balance = beg_balance + deposit_ttl;
      printf ("\n\nThe closing balance is %.2f\n", end_balance);
      if (end_balance >= 50000)
        printf ("*** It is time to invest some money!***\n\n");
      else if (end_balance >= 15000)
        printf ("*** Maybe you should consider a CD. ***\n\n");
      else if (end_balance >= 1000)
        printf ("*** Keep up the good work!***\n\n");
      else if (end_balance >= 0)
        printf ("*** Your balance is getting low!***\n\n");
      printf ("***** Bank Record ******\n");
      printf ("\nStarting Balance $%.2f\n", beg_balance);
      for (i = 0; i < nm_deposits; i++)
        {
          printf ("Deposit #%d : $%.2f\n", i + 1, deposits[i]);
        }
      printf ("\n");
      for (i = 0; i < nm_withdrawals; i++)
        {
          printf ("Withdrawal #%d : $%.2f\n\n", i + 1, withdrawals[i]);
        }
      printf ("Ending balance :$%.2f\n\n", end_balance);
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Enable your compiler's warnings. You're checking the value of beg_balance without initializing it first. You are also comparing the static array deposits address with numbers...

    Btw, your indentation is awful, please fix it. Pick a standard style and stick to it.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Apr 2017
    Location
    Quetzaltenango
    Posts
    82
    You've written deposits where you meant nm_deposits. You've omitted an ampersand off of a scanf variable. This program is much easier to understand if you reduce the scope of the x variable to the "Get Deposit Amounts" section, and the y variable to the "Get Withdrawal Amounts" section. You can reduce the scope of c and i also.
    Code:
    //Get Deposit Amounts
          { int x = 1;
            while...
              } 
            }
          }
          printf ("\n");
    Last edited by christophergray; 04-10-2018 at 10:11 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what i am missing here
    By Satya in forum C Programming
    Replies: 4
    Last Post: 05-18-2015, 04:05 AM
  2. what am i missing?
    By pipskie in forum C Programming
    Replies: 8
    Last Post: 11-26-2012, 07:09 PM
  3. Missing
    By jturner38 in forum C++ Programming
    Replies: 3
    Last Post: 11-30-2010, 11:34 AM
  4. what am i missing here??
    By xabhi in forum C Programming
    Replies: 6
    Last Post: 09-03-2010, 10:03 AM
  5. what am i missing now...
    By MK27 in forum C Programming
    Replies: 8
    Last Post: 09-15-2008, 03:13 AM

Tags for this Thread