c++ - 没有构建错误,错误的输出

标签 c++

我不得不为我的 C++ 类(class)编写这个程序,我认为我没有遇到任何问题,但每次我运行这个程序时,我都会得到乱码作为输出。我步入程序(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;
}

最佳答案

在 Account::Account(double Balance, double InterestRate1, double InterestRate2) 你说

balance = balance;

这应该是:

balance = Balance;

关于c++ - 没有构建错误,错误的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7950280/

相关文章:

c++ - 能否在 O(n) 中识别和量化字符串中的重复字符?

c++ - Objective-C 弧 : is it correct to use a block as C++ callback

c++ - 从内存而不是 URL 动态加载 QML

C++ 将 RGBA 打包为 unsigned int

C++ 实例变量初始值设定项中的重复类型

c++ - 查找汉明数 - 不是代码或距离

c# - 互操作将字符串从 C# 发送到 C++

c++ - cython cmake 模块运行时错误

c++ - 如何将解决方案从 Visual Studio 2010 降级到 Visual Studio 2005?

c++ - Caffe 示例代码中的 "TEST"变量有什么作用?