这是我必须为我的c ++类编写的程序,除了我的输出只显示1个小数位并且我需要它显示两个(例如:2.22,而不是2.2)之外没有任何问题。你们有没有想法我如何格式化这个改变?
// Author:
// Assignment: 7
#include <iostream>
#include <fstream> // I/O
#include <iomanip> // For setw()
using namespace std;
ofstream outputfile("output.txt"); // Output file
const int MAX_FILE_NAME = 35; // Max space allocated for file name
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); // Print values on output stream out();
private:
double balance;
double interest;
double payment;
double interest_rate1;
double interest_rate2;
double total_amount_due;
};
// Utility Functions
void open_input(ifstream& input, char name[]); // Get file name & Open file
void process_file(ifstream& input); // Read each value & process it
int main()
// Parameters: None
// Returns: Zero
// Calls: open_input(), process_file()
{ char again; // Does user want to go through loop again?
char file_name[MAX_FILE_NAME + 1]; // Name of file to be processed
ifstream input_numbers; // For working with input file
cout << "This program computes the interest due, total amount due,\n"
<< "and the minimum payment for a revolving credit account.\n" << endl;
system("pause"); // Hold message on screen until key is pressed
do
{
system("cls"); // Clear screen
open_input(input_numbers, file_name); // Get file name & open file
process_file(input_numbers); // Process values in file
input_numbers.close(); // Close file
cout << "\nDo you want to process another file (Y/N)? ";
cin >> again;
cin.ignore(256, '\n'); // Remove Enter key from keyboard buffer
} while ( again == 'y' || again == 'Y');
cout << "\nEnd of Program!" << endl;
outputfile << "\n\nThanks for using CreditCalculator!\f";
outputfile.close();
return 0;
} // End of main()
void open_input(ifstream& input, char name[]) //Open file, exit on error
// Parameters: Variables for input file reference nad input file name
// Returns: None
// Calls: None
{ int count = 0; // Count number of tries
do // Continue until we get a valid file name and can open file
{ count++;
if (count != 1) // Issue error message if we are trying again.
{ 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);// Gets at most MAX_FILE_NAME characters
cin.ignore(256, '\n'); // Remove Enter key from keyboard buffer
input.clear(); // Clear all error flags, if any, from prev try
input.open(name,ios_base::in); // Open only if file exists
} while (input.fail() ); // If can't open file, try again
} // End of open_input()
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),interest(0.00),payment(0.00),total_amount_due(0.00)
{
balance = Balance;
interest_rate1 = InterestRate1;
interest_rate2 = InterestRate2;
Payment();
total_amount_due = balance + interest;
}
Account::Account()
{
balance = 0.00;
}
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 = 0.00;
if ( balance > LEVEL_ONE_BALANCE)
interest = (balance - LEVEL_ONE_BALANCE) * interest_rate2 + LEVEL_ONE_BALANCE * interest_rate1;
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;
}