How do you split dollars and cents from a net pay?

I have been assigned to plan, code, and execute a program that will input a name and a pay rate from a data file. I have begun my planning and have finished setting up the formatting for the output and all of the equations to find net pay. I am stuck on splitting $55.75 into 55 Dollars & 75 Cents. I have the formatting down to insert the 55 and 75 as declared variables into their respective places(in the format of a check), but I need help with separating 55 and 75. Any help or direction would be appreciated. Thank you.
show some code...

use classes...
I do not have the code yet. I need to plan out the programming before I can start on the code. My question, actually, is what code is necessary to separate the two values and how is that code implemented. I am NOT asking anyone to do my homework. I recently had surgery and fell behind on homework. I was not able to make it to class for the material so now I'm stuck to exploring on my own. I am open to any advice possible. Thank you.
How are you getting this data? As a double?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void example(double& d1)
{
	double cents = 0, dollars = 0;
	while(true)
	{
		if ( cents < d1 )
			cents++;
		else if ( cents > d1 )
			break;
		else if ( cents == d1 )
			return; // no seperation needed
	}
	dollars = cents - 1;
	cents -= d1; 
	cents = (1 - cents)*100;
}
Just take advantage of the built in type conversions

1
2
3
4
5
double money = 55.75;
int dollars = (int)money; // dollars = 55
double cents = money - dollars; // cents = 0.75
//or to get cents as an int value:
int cents = (int)((money - dollars)*100); // cents = 75 
Last edited on
blech

Just use modf: http://www.cplusplus.com/reference/clibrary/cmath/modf/

1
2
3
4
double money = 55.75;
double dollars, cents;

cents = modf(money,&dollars) * 100;
blech

All financial code should use BCD.

Just kidding.
Actualy I'd probably use integers for money and just keep track of the cents instead of the dollars.

1
2
3
int money = 5575;  // not 55.75
int dollars = money / 100;
int cents = money % 100;
Topic archived. No new replies allowed.