C++程序,输出问题

标签 c++

这是我必须为我的 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;
}

最佳答案

您可以使用 setprecision 设置您选择的小数精度值。

关于C++程序,输出问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7963221/

相关文章:

C++ 函数指针名称在函数模板内查找

c++ - 如何在 Clang 中获取 NamedDecl 的错位名称?

c# - 如何将命名管道字符串从非托管代码空间发送到托管代码空间?

c++ - 在执行过程中如何用C++代码在计算机中找到Iostream文件

c++ - 我们应该在通过引用传递之前取消引用指针吗?

c++ - 无法为进程中的所有线程安装 Hook

c++ - 将 2 维 double 组设置为 -1 的最快方法?

c++ - 从 c++ vector 中删除元素,其中删除条件取决于其他元素

c++ - 我如何 "override"CMake中文件的扩展类型

c++ - 如何在库中使用静态函数