c++ - 如何让月份停止显示 1.00、2.00 等

标签 c++

我是 c++ 的新手,我正在编写一个程序来计算三个月期末储蓄账户的余额。我应该使用循环,我已经这样做了并且没有太大问题。我遇到的问题是存款、取款、当前余额等的所有数字都应该显示为 x.xx,我得到了那个输出,但它也在一个月内这样做。我如何才能使月份不显示为 x.xx? 这是我的代码:

#include "pch.h"
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int main()
{
    double startbalance;
    double annualrate;
    double monthlyrate;
    double deposit;
    double withdrawal;
    double totaldeposit = 0;
    double totalwithdrawal = 0;
    double totalinterest = 0;
    double monthstart = 0;
    double monthend = 0;
    printf("Welcome to Your Bank!\n");
    cout << "What is your starting Balance? $";
    cin >> startbalance;
    cout << "What is the annual interest rate?. Please enter whole value. For example 6 for 6% :";
    cin >> annualrate;
    monthend += startbalance;
    for (double month = 1; month <= 3; month++)
    {
        cout << "Month #" << month << endl;
        do
        {
            cout << setprecision(2) << fixed;
            cout << "Current Balance: $" << monthend << endl;
            cout << "Please enter total amount of deposits: $";
            cin >> deposit;

            if (deposit < 0)
            {
                cout << "Deposit must be a positive number!\n";
            }
        } while (deposit < 0);
        totaldeposit += deposit;
        monthend += deposit;
        do
        {
            cout << "Please enter total amount of withdrawals: $";
            cin >> withdrawal;
            if (withdrawal < 0 || withdrawal > monthend)
            {
                cout << "Withdrawal must be a positive number and not be larger than balance: $" << monthend << endl;
            }
        } while (withdrawal < 0 || withdrawal > totaldeposit);
        cout << endl;
        totalwithdrawal += withdrawal;
        monthend -= withdrawal;
        monthlyrate = ((monthstart + monthend) / 2 * (annualrate / 12));
        totalinterest += monthlyrate;
        cout << "New Balance: $" << monthend << "\n";

    }
    cout << endl;
    cout << fixed << showpoint << setprecision(2);
    cout << "Start Balance:         " << setw(9) << "$" << startbalance << "\n";
    cout << "Total Deposits:        " << setw(9) << "$" << totaldeposit << "\n";
    cout << "Total Withdrawals:     " << setw(9) << "$" << totalwithdrawal << "\n";
    cout << "Total Interest Earned: " << setw(9) << "$" << totalinterest << "\n";
    cout << "Final balance:         " << setw(9) << "$" << monthend << "\n";

    return 0;
}

最佳答案

只需在显示之前将您的月份变量类型转换为 int。

cout << "Month #" << (int)month << endl;

这应该可以解决您的问题。

关于c++ - 如何让月份停止显示 1.00、2.00 等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54994749/

相关文章:

c++ - 我如何在 vc6.0 中使用 Imagemagick 库?

C++11 STL 容器和线程安全

c++ - 没有完整路径包括我的图书馆

c++ - 如何在 C++ 中将 float 指针转换为 int 指针?

c++ - 如何找到exe的路径

javascript - 如何将参数传递给 evaluateJavaScript 函数?

c++ - extern "C"---何时*确切*使用?

c++ - QTreeWidgetItem : How can I get the selected item?

c++ - 在 C++ 中,按序号链接和按名称链接是什么意思?

C++使函数调用依赖于模板参数