Help with getting double value to work in functions

Need to write conversion code using functions, but computer returns integers and not double values. Am I missing something???

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
//
//  main.cpp
//  homework 6
//
//  Created by Patrick Stark on 11/7/15.
//  Copyright © 2015 Patrick Stark. All rights reserved.
//

#include <iostream>

using namespace std;

int inchesToFeet(double inches2)
{
    
    
    double feet = inches2 / 12;
    
    return feet;

}

int inchesToYards(double inches2)
{
    

    double yards = inches2 / 36.0;
    
    return yards;
}

int inchesToMeters(double inches2)
{
    
    
    double meters = inches2 * 0.0254;
    
    return meters;
}

int main()
{
    double inches;
    
    cout << "Please enter a length in inches: ";
    
    cin >> inches;
    
    cout << inches << " inches is " << inchesToFeet(inches) << " feet." << endl;
    
    cout << inches << " inches is " << inchesToYards(inches) << " yards." << endl;
    
    cout << inches << " inches is " << inchesToMeters(inches) << " meters." << endl;
    

    return 0;
}
Perhaps you need to review your textbook for information about functions, taking special notice of the return value.

With all of your functions you told the compiler they would be returning an int, therefore the compiler implicitly converted your doubles to int in order to return the type of variable you wanted.

1
2
int inchesToMeters(double inches2)       // Returns an int.
double inchesToMeters(double inches2) // Returns a double. 
Topic archived. No new replies allowed.