Saya seorang siswa di kelas pemrograman dasar dan saya mencoba menyelesaikan program ini untuk tugas kelas. Ini adalah program sederhana yang menghitung bunga majemuk dengan masukan dari pengguna. Namun, ketika menulis kode, saya perhatikan bahwa hasilnya adalah 0 meskipun berdasarkan input yang saya harapkan sebaliknya. Adakah yang bisa memberi tahu saya mengapa program tidak menunjukkan hasil?

#include <iostream>
#include <cmath>
using namespace std;

// Declarations of Prototype
void futureValue(double* presentValue, float* interestRate, int* months, double* value);

// List of variables
double presentValue = 0;
float interestRate = 0;
double value = 0;
int months = 0;

// Start of Main function
int main(void)
{
    cout << "What is the current value of the account?";
    cin >> presentValue;
    cout << "How many months will Interest be added to the account?";
    cin >> months;
    cout << "And what will be the Interest Rate of the account?";
    cin >> interestRate;
    cout << "After " << months << " months, your account balence will be $" << value << ".";
    return 0;
}

void futureValue()
{
    if (presentValue <= 0)
    {
        cout << "I'm sorry, you must have a current balence of more than 0.00 dollars to calculate.";
        return;
    }
    else
    {
        value = presentValue * pow(interestRate + 1, months);
        return;
    }
}

0
DJ July 17 April 2020, 19:36

1 menjawab

Jawaban Terbaik

Iya. Anda tidak memanggil fungsi futureValue yang akan menghitung value untuk Anda. Karena value tidak dihitung, tetap 0. Perbaiki:

#include <iostream>
#include <cmath>
using namespace std;

// Declarations of Prototype
void futureValue(double* presentValue, float* interestRate, int* months, double* value);

// List of variables
double presentValue = 0;
float interestRate = 0;
double value = 0;
int months = 0;

// Start of Main function
int main(void)
{
    cout << "What is the current value of the account?";
    cin >> presentValue;
    cout << "How many months will Interest be added to the account?";
    cin >> months;
    cout << "And what will be the Interest Rate of the account?";
    cin >> interestRate;
    futureValue(); //Here we compute the value
    cout << "After " << months << " months, your account balence will be $" << value << ".";
    return 0;
}

void futureValue()
{
    if (presentValue <= 0)
    {
        cout << "I'm sorry, you must have a current balence of more than 0.00 dollars to calculate.";
        return;
    }
    else
    {
        value = presentValue * pow(interestRate + 1, months);
        return;
    }
}
0
Lajos Arpad 17 April 2020, 17:01