Help with this while loop

This is a discussion on Help with this while loop within the C++ Programming forums, part of the General Programming Boards category; something is wrong with my while loop. it prints the last code twice. it takes 3 numbers from a text ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    2

    Help with this while loop

    something is wrong with my while loop. it prints the last code twice. it takes 3 numbers from a text file and calculates the monthly payment and prints to screen. the problem is it prints the last monthly payment twice.

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <fstream>
    #include <cstdio>
    #include <cmath>
    
    using namespace std;
    
    void usage()
    {
      cout << "Usage: loancalc <filename>" << endl;
      exit (1);
    }
    
    double monthlyPayment (double amount, int months, double interest)
    {
      double ratem, expm, mp;
      ratem = interest / 1200.00;
      expm = 1.0 + ratem;
      mp = (ratem * pow(expm, months) * amount) / ( pow(expm, months) -1.0); 
      return mp;
    }
    
    void displayLoanInfo (double amount, int months, double interest)
    {
      double mp, total;
      printf ("%10.2f %8d %10.2f ",amount, months, interest);
      if (amount > 0 && months > 0 && interest > 0)
      {
        mp = monthlyPayment(amount, months, interest);
        total = mp * months;
        printf ("%11.2f %12.2f \n",mp, total);
      }
      else printf ("    <error>      <error>\n");
    }
    
    int main (int argc, char **argv)
    {
      ifstream infile;
      double d1,d2;
      int i;
     
      if (argc !=2)
      {
        usage ();
      }
    
      infile.open (argv[1]);
    
      printf("                                   Monthly\n");
      printf("    Loan      Months      Rate     Payment      Total\n");
      printf("-----------------------------------------------------------\n");
      
      if (!infile.fail ())
      {
        while (!infile.eof ())
        {
          infile >> d1 >> i >> d2;
          displayLoanInfo (d1, i, d2);
        }
      }
      else cout << "No File!" << endl;
      
      infile.close ();
      return 0;
    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    > it prints the last code twice.
    Because of this probably:
    >while (!infile.eof ())
    You should really be using the return code from the read functions (whatever you're using) to determine when you've hit EOF.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    2
    thanks for replying so quickly
    but I kinda figured it was doing it because it was reading a blank line but I wasnt to sure if that was the reason. should I use another stoping condition in a while loop?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 12:38 PM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-19-2005, 11:30 PM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 08:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21