没有构建错误,输出错误

时间:2011-10-31 05:36:25

标签: c++

我必须为我的C ++类编写这个程序,我不认为我遇到任何问题,但每次运行程序时我都会输出乱码。我已经进入该计划(Visual Studios 2010是我的编译器)并发现程序在这里出错了

Account::Account(double Balance, double InterestRate1, double InterestRate2)
{
balance = balance;
interest_rate1 = InterestRate1;
interest_rate2 = InterestRate2;
Payment();
}

它将余额设置为6(我正在读取的文件中的第一个数字)和利率,但是一旦你进入下一步,一切都会出错。为什么?

#include <iostream>
#include <fstream> 
#include <iomanip> 
using namespace std;

ofstream     outputfile("output.txt");    

const int    MAX_FILE_NAME = 35;           
const double INTEREST_RATE1 = 0.015;
const double INTEREST_RATE2 = 0.010;
const double LEVEL_ONE_BALANCE = 1000.00;
const double MINIMUM_PAYMENT = 10.00;
const double MINIMUM_PAYMENT_PERCENT = 0.10;

class Account
{
public:
Account( double Balance, double InterestRate1, double InterestRate2);
Account( );
double Payment();
double InterestDue();
double TotalAmountDue();
void output(ostream &out); 

private:
double balance;
double interest;
double payment;
double interest_rate1;
double interest_rate2;
double total_amount_due;
};

void open_input(ifstream& input, char name[]); 
void process_file(ifstream& input);            

int main() 

{  char again;             
   char file_name[MAX_FILE_NAME + 1]; 
   ifstream input_numbers;            

   cout << "This program computes the interest due, total amount due,\n"
        << "and the minimum payment for a revolving credit account.\n" << endl;
   system("pause"); 

   do 
   {  
      system("cls");                           
      open_input(input_numbers, file_name);    
      process_file(input_numbers);             
      input_numbers.close();                   

      cout << "\nDo you want to process another file (Y/N)? ";
      cin >> again;
      cin.ignore(256, '\n'); 

   } 
   while ( again == 'y' || again == 'Y'); 

   cout << "\nEnd of Program!" << endl;
   outputfile << "\n\nThanks for using CreditCalculator!\f"; 
   outputfile.close();

   return 0; 
}  



void open_input(ifstream& input, char name[]) 
{  int count = 0;            
   do
   {  count++;
      if (count != 1)
      {  cout << "\n\aInvalid file name or file does not exist. Please try again." 
              << endl;
      }
      cout << "\nEnter the input file name (maximum of " << MAX_FILE_NAME
           << " characters please)\n:> ";
      cin.get(name, MAX_FILE_NAME + 1);
      cin.ignore(256, '\n');           
      input.clear();                   
      input.open(name,ios_base::in); 
   } while (input.fail() );            
} 

void process_file(ifstream& input) 
{  double value;              
   Account thisAccount;             
   while (input >> value)     
   {
      thisAccount = Account(value, INTEREST_RATE1, INTEREST_RATE2);
      thisAccount.output(cout); 
          thisAccount.output(outputfile);   
   }
} 

// Account Class

Account::Account(double Balance, double InterestRate1, double InterestRate2)
    {
balance = balance;
interest_rate1 = InterestRate1;
interest_rate2 = InterestRate2;
Payment();
    }

Account::Account()
{
    balance = 0;
}
double Account::InterestDue()
{ 
    return interest;
}
double Account::TotalAmountDue()
{
    return total_amount_due;
}
void Account::output(ostream &out) // Print values on output stream out();
{
   out << "\n\nBalance   : $" << setw(8) << balance << endl;
   out <<     "Payment   : $" << setw(8) << payment << endl;
   out <<     "Interest  : $" << setw(8) << interest << endl;
   out <<     "Total due : $" << setw(8) << total_amount_due << endl;

}
double Account::Payment()
{

double newbalance;

if ( balance > LEVEL_ONE_BALANCE)
     interest = (balance - LEVEL_ONE_BALANCE) * interest_rate2 +
                         LEVEL_ONE_BALANCE * interest_rate1; (this goes up there ^)
else
     interest = balance * interest_rate1;
newbalance = balance + interest;

payment = newbalance * MINIMUM_PAYMENT_PERCENT;

if (newbalance < MINIMUM_PAYMENT_PERCENT)
     payment=newbalance; 
else
     if ( payment < MINIMUM_PAYMENT)
          payment=MINIMUM_PAYMENT;

return payment;
}

1 个答案:

答案 0 :(得分:4)

在Account :: Account(双倍余额,双倍InterestRate1,双倍InterestRate2)中,您说

balance = balance;

这应该是:

balance = Balance;