c++ - 输出未正确递增 - C++
问题描述
目前正在为 C++ 课程做一个项目,我需要在其中制作一个存款金额表,该金额所赚取的利息以及所赚取的总利息每年都在增加。
我没有得到我正在寻找的输出。它只是给我每年相同的输出。我确信我的问题在于我的 for 循环语句,但我需要一套新的公正的眼睛,因为我是新手。首先是.cpp
#include <iostream>
#include <iomanip>
#include "Investment.h"
using namespace std;
int main() {
//Declare variables for input
double initialDeposit, monthlyDeposit, interestRate, years, months;
//Declare variables for data
double totalInterestEarned;
Investment userInput;
//Display menu to the user
cout << "**********************************" << endl;
cout << "*********** Data Input ***********" << endl;
cout << "Initial Investment Amount: " << endl;
cout << "Monthly Deposit: " << endl;
cout << "Annual Interest: " << endl;
cout << "Number of years: " << "\n" << endl;
//Get user input
cout << "**********************************" << endl;
cout << "*********** Data Input ***********" << endl;
cout << "Initial Investment Amount: $" << endl;
cin >> initialDeposit;
userInput.setInitialDeposit(initialDeposit);
cout << "Monthly Deposit: $" << endl;
cin >> monthlyDeposit;
userInput.setMonthlyDeposit(monthlyDeposit);
cout << "Annual Interest: %" << endl;
cin >> interestRate;
userInput.setInterestRate(interestRate);
cout << "Number of years: " << endl;
cin >> years;
userInput.setNumYears(years);
months = years * 12;
totalInterestEarned = userInput.interestEarned(initialDeposit, interestRate);
//Display year end data if no monthly deposits
cout << endl << "Balance and Interest Without Additional Monthly Deposits" << endl;
cout << "================================================================" << endl;
cout << "Year Year End Balance Year End Earned Interest" << endl;
cout << "----------------------------------------------------------------" << endl;
userInput.grandTotal(totalInterestEarned, initialDeposit, years);
//Display year end data with monthly deposits
cout << endl << "Balance and Interest With Additional Monthly Deposits" << endl;
cout << "================================================================" << endl;
cout << "Year Year End Balance Year End Earned Interest" << endl;
cout << "----------------------------------------------------------------" << endl;
userInput.grandTotalMonthly(totalInterestEarned, initialDeposit, interestRate, years);
return 0;
}
第二个是投资.h
#include <iostream>
#include <iomanip>
#ifndef Investment
using namespace std;
class Investment {
public:
//Data variables needed here for this class
double initialDeposit = 0.0;
double interestRate = 0.0;
double monthlyDeposit = 0.0;
double years = 0.0;
public:
//Constructor here
Investment() {}
void setInitialDeposit(double fromUserInput)
{
initialDeposit = fromUserInput;
}
double getInitialDeposit()
{
return initialDeposit;
}
void setInterestRate(double fromUserInput)
{
interestRate = fromUserInput;
}
double getInterestRate()
{
return interestRate;
}
void setMonthlyDeposit(double fromUserInput)
{
monthlyDeposit = fromUserInput;
}
double getMonthlyDeposit()
{
return monthlyDeposit;
}
void setNumYears(double fromUserInput)
{
years = fromUserInput;
}
double getNumYears()
{
return years;
}
double interestEarned(double initialDeposit, double interestRate) {
double totInterest = (initialDeposit + (interestRate / 100.0));
return totInterest;
}
void grandTotal(double interestEarned, double initialDeposit, double years) {
//Calculate yearly interest and year end total
for (int i = 0; i < years; i++) {
//Calculate yearly interest amount
interestEarned = ((initialDeposit) * (interestRate / 100));
//Calculate year end total
double totalAmount = initialDeposit + interestEarned;
//Show decimal as dollar amount correctly with set precision to 2 decimal places
cout << (i + 1) << "\t\t$" << fixed << setprecision(2) << totalAmount << "\t\t\t$" << interestEarned << endl;
}
}
void grandTotalMonthly(double interestEarned, double intiialDeposit, double interestRate, double years) {
for (int i = 0; i < years; i++) {
//Initialize yearly interest to 0
double yearlyTotalInterest = 0.0;
double totalAmount = initialDeposit;
for (int j = 0; j < 12; j++) {
//Calculate monthly interest amount
interestEarned = (((initialDeposit + monthlyDeposit) * (interestRate / 100)) / 12);
//Calculate month end interest
yearlyTotalInterest = yearlyTotalInterest + interestEarned;
//Calculate month end total
totalAmount = totalAmount + monthlyDeposit + interestEarned;
}
cout << (i + 1) << "\t\t$" << fixed << setprecision(2) << totalAmount << "\t\t\t$" << yearlyTotalInterest << endl;
}
}
};
#endif
为清楚起见,以下是当前错误的程序输出。
**********************************
*********** Data Input ***********
Initial Investment Amount: $
1000
Monthly Deposit: $
5
Annual Interest: %
5
Number of years:
5
Balance and Interest Without Additional Monthly Deposits
================================================================
Year Year End Balance Year End Earned Interest
----------------------------------------------------------------
1 $1050.00 $50.00
2 $1050.00 $50.00
3 $1050.00 $50.00
4 $1050.00 $50.00
5 $1050.00 $50.00
Balance and Interest With Additional Monthly Deposits
================================================================
Year Year End Balance Year End Earned Interest
----------------------------------------------------------------
1 $1110.25 $50.25
2 $1110.25 $50.25
3 $1110.25 $50.25
4 $1110.25 $50.25
5 $1110.25 $50.25
解决方案
我对利率了解不多,但grandTotal
你可能忘记了
//Calculate yearly interest amount
interestEarned = ((initialDeposit) * (interestRate / 100));
它应该是
//Calculate yearly interest amount
interestEarned = ((initialDeposit) * std::pow(interestRate / 100, i));
随着兴趣逐年增长
推荐阅读
- android - 屏幕的某些部分没有捕捉到手势(即滑动、滚动、点击...)
- c++ - 在 omnet++ 中到达目的地时无法删除味精?
- scala - 定义隐式方法时错误的顶部语句声明错误
- javascript - 如何制作像维基百科(JavaScript)这样的悬停效果?
- python-3.x - 我在哪里可以找到 pyc 文件?
- ios - 快速均衡tableView滚动的位置
- javascript - 对 mongoose 返回的结果使用 lodash 时输出不正确
- java - Spring Boot 数据 r2dbc 自动创建表
- html - Web Scrape:如何从所有链接中获取所有名称和价格?
- flutter - 如何将数据表转换为 listview.builder?