Thread: runtime errors

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    60

    runtime errors

    I wrote this program that displays the balance with interest after the number of years requested. When I run the program and say enter 75.00 for the "dollar amount deposited", the program skips over the input for interest and goes right to the input for years. If I enter "75" for the "dollar amount deposited", it will go to the input line for interest. Also, both scenerios produce wrong results. What did I do wrong? Here is my code:

    Code:
    /* 
       Filename:    balance.c
    
       Description: Displays the balance with interest compounnded annually, 
                    after number of years entered.
    */
    
    # include <stdio.h>
    # include <math.h>
    
       void introduction (void);
    
       double ComputeBalance (double a, double r, int n);
    
    
     int main()
    
      {
      
      /*Allocate memory for data*/
    
       introduction ();
       
       double a,r;
    
       int n;
    
      /*Prompt user for data*/
    
         printf ("\nEnter the the dollar amount deposited: $");
         scanf ("%.2lf",&a);
    
         printf ("\nEnter the interest rate:");
         scanf ("%lf",&r);
    
         printf ("\nEnter the number of years:");
         scanf ("%d",&n);
    
      /*Processing and display results*/
    
         printf ("\nYour balance after %d years with an\n");
    
         printf ("interest rate of %lf would be");
     
         printf (" $%.2lf\n",n,ComputeBalance(a,r,n));
    
     return 0;
     
      }
    
         void introduction (void)
    
          {
    
           printf ("\nThis program displays the balance with interest,\n");
           printf ("compounded annually after number of years entered.\n");
    
          }
    
         double ComputeBalance(double a,double r, int n)
    
          {
    
             return a*1+pow(r,n);
    
          }

  2. #2
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    Not all input gets caught by scanf only those you need. And those left will stay in your input stream until the next scanf or input. So the next call of scanf will catch the previous input that your previous scanf didnt get.

    call this function after evry scanf(); This will remove all the junks left by previous inputs.

    Code:
    void flush()
    {
          int c;
    
          while ( ( c = getchar() ) != '\n' && c != EOF );
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. global namespace errors
    By stubaan in forum C++ Programming
    Replies: 9
    Last Post: 04-02-2008, 03:11 PM
  2. FILES in WinAPI
    By Garfield in forum Windows Programming
    Replies: 46
    Last Post: 10-02-2003, 06:51 PM
  3. opengl Nehe tutorial errors when compiling
    By gell10 in forum Game Programming
    Replies: 4
    Last Post: 07-14-2003, 08:09 PM
  4. executing errors
    By s0ul2squeeze in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2002, 01:43 PM