I am somewhat new to C++ and and when I try to run the following code
main.cpp:
#include <iomanip>
#include <string>
#include <limits>
#include "console.h"
using namespace std;
double calculate_future_value(double monthly_investment, double yearly_interest_rate, int years);
int main()
{
cout << "The Future Value Calculator\n\n";
char choice = 'y';
while (tolower(choice) == 'y')
{
cout << "INPUT\n";
double monthly_investment =
console::get_double("Monthly Investment: ", 0, 10000);
double yearly_rate =
console::get_double("Yearly Interest Rate: ", 0, 30);
int years =
console::get_int("Years ", 0, 100);
cout << endl;
double future_value = calculate_future_value(monthly_investment,
yearly_rate, years);
cout << "OUTPUT\n"
<< fixed << setprecision(2)
<< "Monthly Investment: " << monthly_investment << "\n"
<< fixed << setprecision(1)
<< "Yearly Interest Rate: " << yearly_rate << "\n"
<< "Years: " << future_value << "\n\n"
<< "Future Value: " << future_value << "\n\n";
choice = console::get_char("Continue? (y/n): ");
}
cout << "Bye!\n\n";
}
double calculate_future_value(double monthly_investment, double yearly_interest_rate, int years)
{
double monthly_rate = yearly_interest_rate / 12 / 100;
int months = years * 12;
double future_value = 0;
for (int i = 0; i < months; ++i)
{
future_value = (future_value + monthly_investment) *
(i + monthly_rate);
}
return future_value;
}
and
console.h:
#define PANDA_CONSOLE_H
#include <string>
#include <limits>
namespace console
{
double get_double(std::string prompt,
double min = std::numeric_limits<double>::min(),
double max = std::numeric_limits<double>::max());
int get_int(std::string prompt,
int min = std::numeric_limits<int>::min(),
int max = std::numeric_limits<int>::max());
char get_char(std::string prompt,
bool add_blank_line = true);
}
#endif
I would get the following error
Undefined symbols for architecture x86_64:
"console::get_double(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, double, double)", referenced from:
_main in main-f6ee4e.o
"console::get_int(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, int, int)", referenced from:
_main in main-f6ee4e.o
"console::get_char(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, bool)", referenced from:
_main in main-f6ee4e.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I've tried moving the definition of calculate_future_value() after using namespace std; and before double calculate_future_value(double monthly_investment, double yearly_interest_rate, int years); and because that didn't work, I am now confused to what I need to do. Please help. Thank You!