Jump to content
  • Advertisement
Sign in to follow this  
emforce

simple math error? c++

This topic is 3498 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, so far i have this for a simple financial calculator program and was wondering why my output does not give me what i expect for example. i enter 100 at 15% for 1 year i expect 115 back but i get some ridiculous number like -9.34383e + 063. what is wrong with 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>
#include <cmath>


using std::cin;
using std::cout;

// program is going to contain many financial calculators
// for instance cost of living calculators
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;
	double bank_balance_multiplier;
	
	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";
	cout << " please enter your choice ";
	cin >> choice;

	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;

		bank_balance_multiplier = std::pow((1+interest_per_year_as_decimal),(num_of_years));
			final_bank_balance = (bank_balance_multiplier * initial_ammount);


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

	case '2' :

		cout << " this is in progress ";
		cout << "\n";
		break;



	

	}


	cout<< " please enter the initial ammount you wish to save ";
	cin >> initial_ammount;

    std::cout << "Press enter to continue" << std::endl;
	std::cin.get();

}

Share this post


Link to post
Share on other sites
Advertisement
A good programming practice you should get into is initializing your variables upon declaring them otherwise expect to get garbage answers like the one you are getting;)

Share this post


Link to post
Share on other sites
i thought i did. i was fiddling around trying to get the pow() workin i must have left it out. but thanks for the advice :D

and thanks for helping haegarr

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!