簡體   English   中英

Output 未正確遞增 - C++

[英]Output not incrementing correctly - C++

目前正在為C++的課程做一個項目,我需要在其中制作一個存款金額表,該金額所賺取的利息以及所賺取的總利息每年都在增加。

我沒有得到我正在尋找的 output。 它每年只給我相同的 output。 我確信我的問題在於我的 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

下面是當前的,為了清楚起見,程序的 output 是錯誤的。

**********************************
*********** 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));

隨着興趣逐年增長

正如@Sho 向我指出的那樣,我沒有將年份 (i) 考慮在內,這導致我解決了 output 問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM