modf function discrepany

Hi all,

I need to separate the pre and post decimal digits from a variable of type double.

I wrote the following as a test which works fine:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <cmath>
int main()
{
double number = 1.6764;
double part1, part2;

part2 = modf(number, &part1);

std::cout << part1 << " and " << part2;

return 0;
}


So this would output "1 and 0.6764"

I am trying to learn c++ using the book "Absolute C++" by Walter Savitch. I have implemented this modf function into one of the projects from the book as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <cmath>
using namespace std;

void input(int& inFeet, int& inInches);
//two parameters of type int, no restriction on values other than that of type int
//inFeet is number of feet
//inInches is number of inches

void conversion(int conFeet, int conInches, double& conMetres, double& conCentimetres);
//4 parameters, 2 of type int and two of type double
//conFeet is number of feet user has input
//conInches is number of inches user has input
//conMetres is the toal number of inches from the sum of conFeet and conInches converted
to metres
//conCentimetres is the remaining decimal from conMetres

void output(double outMetres, double outCentimetres);
//two parameters of type double
//outputs the users input of feet and inches in metres and centimetres

int main()
{
int feet, inches;
double metres, centimetres;
char ans;

cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);

do
{
input(feet, inches);
conversion(feet, inches, metres, centimetres);
output(metres, centimetres);

cout << "Again (Y/N)? ";
cin >> ans;
}while( ans == 'y' || ans == 'Y');

return 0;

void input(int& inFeet, int& inInches)
{
cout << "Please input your length in feet (asked for inches next): ";
cin >> inFeet;
cout << "and now the remaining inches: ";
cin >> inInches;
}

void conversion(int conFeet, int conInches, double& conMetres, double& conCentimetres)
{
const double metresToFeet = 0.3048;
const double inchesToCm = 2.54;
double totalCm, totalM;

totalCm = (conInches + (12 * conFeet)) * inchesToCm;
totalM = totalCm/100;

conCentimetres = modf(totalM, &conMetres) * 100;
void output(double outMetres, double outCentimetres)
{
cout << "This is the equivalent of " << outMetres << "M " << outCentimetres
<< "cm" << endl;
}


The section of code in question is highlighted in bold

Unlike the test program this outputs the pre decimal digit as a double (So in the instance of the test program the output is "1.00 and 0.6764"). Can someone explain to me why the first program does not.

If any of this is not clear just ask and I will try and explain myself better

Thanks for any help you can give

Chris
The modf() function gave you correct results -- which you have already observed.

The difference is the way you are printing the results. Look at the <iomanip>ulators for more.
Topic archived. No new replies allowed.