我如何让这个月停止显示1.00、2.00,..等
How do I get the month to stop displaying 1.00, 2.00, ... etc
我是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之类的小数的数字,float
是double
示例:
//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.0
,2.0
等。
将其更改为:
for ( int month = 1; month <=3; month++ ) {
cout << "Month #" << month << endl;
逻辑上,变量月应该是整数。将其数据类型声明为 int 而不是 double 。
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有人能分解一下这个c++模板的语法吗
- 为什么这个运算符<重载函数对 STL 算法不可见?
- 为什么这个 for 循环总是显示"o"。C++
- 我正在调试这个C++程序.编译器不再显示语法错误,但存在隐藏的逻辑错误
- 为什么以下代码在阵列中显示上个月的最后一个月,而不是显示降雨量最高和最低降雨量的月份
- 为什么这个 while 循环不显示第一个元素?
- 谁能解释这个代码?为什么显示 1
- 我如何让这个月停止显示1.00、2.00,..等
- 如何增加几个月循环.然后显示
- 如何显示与系统不同的区域设置的QDate月
- 为什么这个代码在我的机器上显示正确的值时却显示一些垃圾类型的值
- 为什么这个代码在找到搜索词后不突出显示?
- 我写了这个程序来反转链表中的元素,在编译这个程序后,在reverse()中显示错误.为什么
- 为什么在这个程序的执行过程中,y从来不显示为1 ?
- 有人能帮助我如何从类中调用这个函数参数以显示在我的输出中吗
- 为什么当我把代码放在课堂上时,这个 richEdit 窗口没有显示?
- 为什么这个C++Trie实现显示出奇怪的行为
- 为什么这个哈希表函数显示编译错误