如何获取停止显示1.00、2.00等的月份

时间:2019-03-05 02:55:32

标签: 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;
}

5 个答案:

答案 0 :(得分:2)

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

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

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

答案 1 :(得分:0)

您可以将monthend设置为int或long。 .........

答案 2 :(得分:0)

请将您的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

答案 3 :(得分:0)

您的变量类型似乎可以进行计算;我相信您的问题出在此声明内:

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;

答案 4 :(得分:0)

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