Расчеты не будут отображаться в выводе - PullRequest
0 голосов
/ 17 апреля 2020

Я учусь в базовом c классе программирования, и я пытаюсь завершить эту программу для задания класса. Это простая программа, которая вычисляет сложный процент по данным пользователя. Тем не менее, при написании кода я заметил, что результат равен 0, хотя в зависимости от ввода я ожидал иначе. Может кто-нибудь сказать мне, почему программа не показывает результаты?

#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;
    }
}

1 Ответ

0 голосов
/ 17 апреля 2020

Да. Вы не вызываете функцию futureValue, которая вычислила бы для вас value. Из-за того, что value не вычисляется, оно остается равным 0. Исправление:

#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;
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...