feet and inches to cm and meters conversion

Hi all,
so I'm taking online C++ at my college right now, and currently I have a project of converting feet and inches to cm and meters, and we have to have 3 functions according to the professor. The thing is all of the variables are int by default, and I cannot get the correct output for the centimeters, because I can't round them up using round() as it's not part of the lessons yet. This makes it a little harder for me to do the project. Could someone guide me to the right path? I'm stuck. Here is what I have so far for my code.

For example, in the professor's example, if we put in 4 feet and 5 inches, we get output of 1 meter(s) and 35 centimeters. But with my code, I only get 34 centimeters as the result.

Another example, if he enters in 6 feet 7 inches, he gets 2 meter(s) and 1 centimeter. But for mine, I get 0 centimeter. Please Help :(

P/s: Requirement from Professor: MUST USE ONLY the conversion factor between feet and meters of 0.3048 meter/feet along with 12 inches in a foot and 100 centimeters in a meter.

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
//Program name:			Convert to Meters Centimeters
//Program Description:	This program will convert feet and inches into meters and centimeters from user input
//Programer Name:		Joey Pham
//Date:					07/13/2019
#include <iostream>
#include <iomanip>
using namespace std;


//function prototype
void getFeetInches(int &feet, int &inches);
void covertToMeterCentimeter(int feet, int inches, int &meters, int &centimeters);
//void displayResults(int feet, int inches, int meters, int centimeters);

int main()
{
	int		feetValue,
			inchesValue,
			metersValue,
			centimetersValue;
	char	again;

	getFeetInches(feetValue, inchesValue);
	covertToMeterCentimeter(feetValue, inchesValue, metersValue, centimetersValue);
//	displayResults(int feet, int inches, int meters, int centimeters);
};



/************************************************************************************
 * 								getFeetInches										*
 * Gets input from user for the amount of feet and inches that needed to be			*
 * converted.
 ************************************************************************************/
void getFeetInches(int &feet, int &inches)
{
	cout << "Enter feet: ";
	cin >> feet;
	cout << "Enter inches: ";
	cin >> inches;
};



void covertToMeterCentimeter(int feet, int inches, int &meters, int &centimeters)
{
	double metTemp, centTemp;
	
	metTemp = (feet+(inches/12.0))*0.3048;
	meters = metTemp;
	centTemp = (metTemp*100) - 100;
	centimeters = centTemp;


	cout << meters << " meter(s) and " << centimeters << " centimeter" << endl;
};

/*
void displayResults(int feet, int inches, int meters, int centimeters)
{

};
*/
Last edited on
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
#include <iostream>

const int CMS_PER_METRE = 100.0 ;
const int INCHES_PER_FOOT = 12.0 ;
const double METRES_PER_FOOT = 0.3048 ;

// slightly crude function to get the nearest integer value
long long round_it( double n )
{
    return n<0 ? n - 0.5 : n + 0.5 ;
}

void convert_to_metric( int feet, int inches, int& metres, int& cms )
{
    const double CMS_PER_FOOT = METRES_PER_FOOT * CMS_PER_METRE ;
    const double CMS_PER_INCH = CMS_PER_FOOT / INCHES_PER_FOOT ;

    const double total_cms = feet * CMS_PER_FOOT + inches * CMS_PER_INCH ;
    const long long total_cms_rounded = round_it(total_cms) ;

    metres = total_cms_rounded / CMS_PER_METRE ;
    cms = total_cms_rounded % CMS_PER_METRE ;
}

void convert_to_archaic( int metres, int cms, int& feet, int& inches )
{
    const double INCHES_PER_METRE = INCHES_PER_FOOT / METRES_PER_FOOT ;
    const double INCHES_PER_CM = INCHES_PER_METRE / CMS_PER_METRE ;

    const double total_inches = metres * INCHES_PER_METRE + cms * INCHES_PER_CM ;
    const long long total_inches_rounded = round_it(total_inches) ;

    feet = total_inches_rounded / INCHES_PER_FOOT ;
    inches = total_inches_rounded % INCHES_PER_FOOT ;
}

int main()
{
    int feet = 4 ;
    int inches = 5 ;
    int metres = 0 ;
    int cms = 0 ;

    convert_to_metric( feet, inches, metres, cms ) ;
    std::cout << feet << "' " << inches << '"' << " == "
              << metres << "m " << cms << "cm\n" ;

    feet = inches = 0 ;
    convert_to_archaic( metres, cms, feet, inches ) ;
    std::cout << metres << "m " << cms << "cm == "
              << feet << "' " << inches << '"' << '\n' ;
}

http://coliru.stacked-crooked.com/a/de68897b3bc759a2
thank you very much, sir!
Actually, would you be so kind to explain why you use long long in the round_it function?
As well as how that function itself works?

It's so simple but so hard for me to grasp the concept at the moment.

Thank you in advanced.
The return type of the function is an integer type. I used long long instead of int to minimise the chance of the number being outside the range of values that the integer type can hold.
You may want to ignore this nicety for now.

When a floating point value is converted to an integer value, the fractional part is discarded.
The idea is to round to the nearest integer value;
so we add 0.5 to non-negative values, and subtract 0.5 from negative values.

A few examples:

3.48 + 0.5 == 3.98 converted to an integer yields 3

3.54 + 0.5 == 4.04 converted to an integer yields 4

-3.48 - 0.5 == -3.98 converted to an integer yields -3

-3.54 - 0.5 == -4.04 converted to an integer yields -4
Topic archived. No new replies allowed.