I am trying to create a program that converts Metric to English units

Here's an example of what my professor wants:

1. Convert length form Metric to English
2. Convert length form for English to Metric
3. Quit the program

Please enter your choice: 1
Enter a number of meters as a double: 75
75.00 meters corresponds to 246 feet, 0.76 inches

My program almost does this. But my output comes out as only a decimal. For example, my output would be "75 meters corresponds to 246.063 feet"

How do I set this up so it will show feet and inches?

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
#include <iostream>
using namespace std;

int main(void)
{
    int number;
    double lengthMet, lengthEng;
    cout << "1.         Convert length from Metric to English" << endl;
    cout << "2.         Convert length from English to Metric" << endl;
    cout << "3.         Quit the program" << endl;

    cout << endl;
    cout << "Please enter your choice: ";
    cin >> number;

    if (number == 1)
    {
        cout << "Enter the number of meters as a double: ";
        cin >> lengthMet;

        lengthEng = lengthMet / 0.3048;

        cout << lengthMet << " meters corresponds to " << lengthEng << " feet";
    }
}
modf found in the <cmath> header can split floating point numbers into integer- and fractional parts:

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
#include <iostream>
#include <cmath>

int main() {

	const double feet_per_meter = 3.28084;
	const double inches_per_foot = 12.0;

	double meters = 75.0;
	
	//feet_whole = 246.063;
	double feet_whole = (meters * feet_per_meter);

	double feet = 0;
	double inches = 0.0;

	//fraction = 0.063;
	//feet = 246;
	double fraction = ::modf(feet_whole, &feet);
	
	//inches = (0.063 * 12.0) = 0.756;
	inches = (fraction * inches_per_foot);

	std::cout << meters << " meters is " << feet << " feet, " << inches << " inches." << std::endl;

	return 0;
}
Last edited on
Thanks so much for your response. But I don't think I'm allowed to use anything other than:
#include <iostream>
using namespace std;
as a header in this class. I don't think my professor allows <cmath> in this intro class.
Is there a way to do this using only <iostream>?
1
2
3
4
5
6
7
8
9
10
11
12
    if (number == 1)
    {
        cout << "Enter the number of meters as a double: ";
        cin >> lengthMet;

        lengthEng = lengthMet / 0.3048;
        
        int feet = lengthEng; ///
        double inches = (lengthEng - feet) * 12.0; ///

        cout << lengthMet << " meters corresponds to " << feet << " feet " << inches << " inches";
    }
Another strategy is to first convert the metres into (integer) one hundredth of inches.

Then feet=value/1200
Thank you vin! This really helped out!
closed account (48T7M4Gy)
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
#include <iostream>

using namespace std;

int main(void)
{
    int feet = 0;
    double inches = 0;
    double metres = 0;

    int number;
    
    cout
    << "1.         Convert length from Metric to English\n"
    << "2.         Convert length from English to Metric\n"
    << "3.         Quit the program\n"
    << '\n'
    << "Please enter your choice: ";
    cin >> number;
    
    if (number == 1)
    {
        cout << "Enter the number of meters as a double: ";
        cin >> metres;
        
        inches = metres * 1000 / 25.4; // 1" = 25.4 mm exactly
        feet = inches/12;
        inches = inches - feet * 12;
        
        cout << metres << " meters corresponds to " << feet << " feet " << inches << " inches\n";
    }
}

1.         Convert length from Metric to English
2.         Convert length from English to Metric
3.         Quit the program

Please enter your choice: 1
Enter the number of meters as a double: 75
75 meters corresponds to 246 feet 0.755906 inches
Program ended with exit code: 0
Subject to further rounding if required.
Last edited on
Topic archived. No new replies allowed.