Jump to content
  • Advertisement
Sign in to follow this  
emforce

to the power of

This topic is 3499 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hello i need help. i have a simple financial calculaor as a small project i am working on to help learn the language of C++ and was wondering. how to calculate compound interest when i use the formulae 20000 * 1.06 to the power of 20. but how do show " to the power of " in my program?

/* this program shows the user how much they could potentially make on interest 
on how much money they are wishing to initially invest.*/

#include <iostream>


using namespace std;

int main ()
{
	double initial_ammount;
	double interest_per_year;
	double final_bank_balance;
	double overall_interest_earned;
	char choice;
	double num_of_years;
	double interest_per_year_as_decimal;
	
	
	cout << " hello and welcome to my finance calculator ";
	cout << "\n";
	cout << " if you want to see how much money you would have in the bank after a certain ammount of years ";
	cout << "\n";
	cout << " please enter 1 ";
	cout << "\n";

	switch(choice)
	{
	case '1' :
		

		cout << " please enter the ammount of money you wish to initially save ";
		cin >> initial_ammount;
		cout << " please enter how much interest you get per year (between 1 and 15 %) ";
		cin >> interest_per_year;
		cout << "please enter the number of years you wish to save for ";
		cin >> num_of_years;

			interest_per_year_as_decimal = (interest_per_year / 100) + 1;
			final_bank_balance = (initial_ammount * interest_per_year_as_decimal)^num_of_years;

		cout << " your final bank balance = " << final_bank_balance << " pounds ";
		break;

	

	}

Share this post


Link to post
Share on other sites
Advertisement
Kind of off topic, but for someone learning C++, before you really start using 'using namespace std;', a good practice is knowing what is actually in the std namespace. For instance, the only thing I see in your program right now is std::cout and std::cin. A lot of people on here will tell you that using a 'using' command for a whole namespace is bad practice, personally, in the program you are running right now I would just us 'using std::cout; using std::cin;'.

Share this post


Link to post
Share on other sites
i thought if i put in using namespace std; i would be saving time on writing. can i ask why its bad practice i am very new to c++ programming (less than week)

and how would i incorporate pow() as number of years?
for instance if i input 5 years how would i make that to the power of 5?

Share this post


Link to post
Share on other sites
For the simple project that you are using right now, it's not a bad practice at all. But once you get into larger projects, you will find yourself overloading operators. And when that happens, you will have cout (as an example) doing more than just sending output. That way you can use cout (the one that you initialized), as well as std::cout.

With that said, I'm not trying to say that you should use it, but a bad way of learning (I found out the hard way) is to use that as a crutch and not be able to remember exactly what all is in the std namespace. For instance, I still try to use std::vector and std::string without the reference to the namespace on multiple occasions, leaving my code very erroneous. That's why I say that you should use 'using std::cout' instead of calling the whole namespace, that way you remember everything that you are using.

Share this post


Link to post
Share on other sites
Ah ha, one of the few Glaswegian GDNeters! [smile]

Quote:
Original post by emforce
i thought if i put in using namespace std; i would be saving time on writing. can i ask why its bad practice i am very new to c++ programming (less than week)
Because everything in the std namespace gets pulled up to global scope. For instance, if you were to #include <vector> with your using namespace std; directive, you'd be unable to use the word vector anywhere after the directive without causing compile errors (Nitpick: I'm sure someone will point out cases where that isn't true [smile]).
It's far preferable to specify a specific class you wank to bring out of the namespace, like:
using std::cin;
using std::cout

That'd mean you don't have to write std::cin or std::cout, and could use it without the std:: prefix, just as you are in your code at the moment.

Quote:
Original post by emforce
and how would i incorporate pow() as number of years?
for instance if i input 5 years how would i make that to the power of 5?
[/quote]
final_bank_balance = std::pow(initial_ammount * interest_per_year_as_decimal, num_of_years);

Share this post


Link to post
Share on other sites
Quote:
Original post by Evil Steve
final_bank_balance = std::pow(initial_ammount * interest_per_year_as_decimal, num_of_years);

With that equation if you start with 2 dollars with 5% interest per year, you would have a final account balance of 0.01 dollars after 2 years.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
Quote:
Original post by Evil Steve
final_bank_balance = std::pow(initial_ammount * interest_per_year_as_decimal, num_of_years);

With that equation if you start with 2 dollars with 5% interest per year, you would have a final account balance of 0.01 dollars after 2 years.


This equation is consistent with the way banks now have to operate to stay afloat [smile]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!