我如何让这个月停止显示1.00、2.00,..等

How do I get the month to stop displaying 1.00, 2.00, ... etc

本文关键字:显示 这个月      更新时间:2023-10-16

我是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;
}

仅在显示之前将您的月变量键入您的月份变量。

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

应该解决您的问题。

您可以使月末或长时间。.........

请将month的数据类型作为int而不是double

double是浮点数据类型。int是一个像1、2、3、4之类的整数。Double是具有1.1或45.564之类的小数的数字,floatdouble

的更具体版本

示例:

//if you just going to work with whole numbers
int a;
a = 1
//if you're working with numbers with a bit more precision
float b;
b = 1.1234
//if you're working with numbers with massive precision.. 
double c;
c = 1.123456

您的变量类型对于计算似乎很好;我相信您的问题在此陈述中:

for (double month = 1; month <= 3; month++) {
    cout << "Month #" << month << endl;

它在您的循环中,就像您的月份打印出来的原因一样:1.02.0等。

将其更改为:

 for ( int month = 1; month <=3; month++ ) {
     cout << "Month #" << month << endl;

逻辑上,变量月应该是整数。将其数据类型声明为 int 而不是 double