gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-27 23:28:01 26 4
gpt4 key购买 nike

我不得不为我的 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com