Archived

This topic is now archived and is closed to further replies.

TheOne1

getting decimals

Recommended Posts

How do I get it so that the numbers after the decimal point don't become 0? If you enter the compound monthly function and enter the required numbers, the result always has the two 00's at the end. How do I get it so there are cents(accurate)?
#include <iostream>

using std::cout;
using std::cin;
using std::endl;
using std::ios;

#include <iomanip>

using std::setiosflags;
using std::setprecision;

#include <cmath>
#include <stdlib.h>

// functions

int compoundMonthly ();
int compoundContinuous ();
int halfLife ();

// global variables

float principle;	// principle (what you started with. (e.g. $500.00)

int	  n;			// # of compounds a year

int	  time;			// # of years

float rate;			// rate


////////////////////////////////


int main()
{
	int HLanswer,		// answer to halfLife() equation

	    choice;			// amount of compound interest for monthly/continously

	float amount;		// choice for menu

	bool exit = 1;
	
	cout << setiosflags( ios::fixed | ios::showpoint )
		 << setprecision( 2 );

	while ( exit == 1) {
		cout << "Choose a function\n" << "1)Compound monthly\n"
			 << "2)Compound continously\n" << "3)Half-Life\n" << "4)Exit\n\n";
		cout << "Enter number choice: ";
		cin >> choice;

		switch ( choice ) {

		case 1:
			amount = compoundMonthly();
			cout << "\nCompound monthly is " << amount << "\n";
			break;

		case 2:
			amount = compoundContinuous();
			cout << "\nCompound interest continuously is: " << amount << "\n";
			break;

		case 3: 
			HLanswer = halfLife();
			cout << "\nHalf-Life of object is: " << HLanswer << "\n";
			break;

		case 4:
			exit = 0;
			break;

		default:
			cout << "\nNot a choice. Choose again.\n";
			break;
		}
	}

	system ( "pause" );

	return 0;
}


// function definitions

int compoundMonthly()
{

	float amount; 

	cout << "\nEnter principle(amount you started with): ";
	cin >> principle;
	cout << "\nEnter rate: ";
	cin >> rate;
	rate = rate * .01;
	cout << "\nEnter number of compounds in a year: ";
	cin >> n;
	cout << "\nEnter number of years: ";
	cin >> time;

	amount = principle * pow( 1.0 + (rate/n), n * time ); 

	return amount;
}

int compoundContinuous()
{
	int e = 2.7;		// always equals 2.7

	float amount;

	cout << "\nEnter principle(amount you started with): ";
	cin >> principle;
	cout << "\nEnter rate(e.g. 7.2% --> 7.2): ";
	cin >> rate;
	rate = rate * .01;
	cout << "\nEnter number of years: ";
	cin >> time;

	amount = principle * pow( e, rate * time );
	
	return amount;
}
//not finished

int halfLife()
{
	return 0;
}
[edited by - TheOne1 on January 6, 2004 8:32:22 PM]

Share this post


Link to post
Share on other sites
Well, first of all, if you want precision, then you shouldn't return an integer with the compountMonthly().

Change the definition and declaration of compoundMonthly() to


float compoundMonthly();


Because when you pass a floating-point value to an integer type, the compiler cuts off the decimals, thus a value like 12.903 becomes 12.00. I'd suggest doing the same to the compoundContinuous() function as well:


float compoundContinuous();


Edit: Typo.

[edited by - biovenger on January 6, 2004 8:52:11 PM]

Share this post


Link to post
Share on other sites
Never, Ever store currency in a float, or double. For anything having to do with currency, you have to use some Decimal type numeric (which is a fixed-point format). Typically, you''ll do this down to 1/1000th of a cent, in effect giving you 5 digits of precision after the fixed decimal point.

For experimentation purposes only, you might want to use "double" for currency, but if you ever stick that in a program and ship it, your program will be used by someone for real money, and you''ll probably get sued because it won''t work right. Remember that.

Share this post


Link to post
Share on other sites
thank you. it is working now....

so hplus (thank you for the info btw), if I were to make a program that does compound interest, storing amount in a float or double would be an error (because amount is for storing currency)?

Share this post


Link to post
Share on other sites