Converting meters to yards, feet, and inches

Can some one review my program an see if it is doing the math correctly. I think it is running right but I would like to get a second opinion if I could. The function of the program is to convert meters, into yards, feet, and inches.
Thank you for the help.

I am using visual basic 2013.

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
#include "stdafx.h"
#include <iostream>
using namespace std;


int inputMeters, yards, feet;
double inches;

int main()
{
	cout << "Enter in a length in meters to be converted into yards, feets, and inches. " << endl;
	cin >> inputMeters;
	inches = inputMeters * 39.37;
	while (inches > 12)
	   {
                if (inches > 12)
			inches = inches - 12;
		        feet++;
		if (feet > 3)
			feet = feet - 3;
			yards++;
	   }
		yards = yards / 3;

	cout << "The length in meters you have entered is converted to " << yards << " " << feet << " and " << inches << endl;

	return 0;
}
Last edited on
I don't know much about yards, feet, and inches but I think something is wrong because when I input 7 meters I get negative inches.

Another thought is that you might want to allow the user to enter the meters as a decimal number. All you would have to do is to change the type so it's very easy.
I just changed it, when I enter 7 I get 7 yards 1 foot and 11.59 inches
You don't want to use a loop. Integer division should do just fine.

Lets say that I have 42,5 fobs and one gek is 13 fobs.
static_cast<int>(42,5) / 13 == 3
In other words, 42,5 fobs contains 3 gek. 3 gek == 39 fobs.
42,5 - 39 == 3,5
Answer: 3 gek and 3,5 fobs.

Determine the yards first. Determine the feet from the remainder. What is left, is inches.
Okay thank you, I didn't even think to use % to get the remainder an go from there!
Not quite sure how to get inches.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "stdafx.h"
#include <iostream>
using namespace std;

int inputMeters, yards, feet, inches;

int main()
{
	cout << "Enter in a length in meters to be converted into yards, feets, and inches. " << endl;
	cin >> inputMeters;

	yards = inputMeters * 1.093;
	feet = static_cast<int>(yards) % 3;
	inches = static_cast<int>(feet) % 12;
	
	cout << yards << " " << feet << " " << inches << endl;
	return 0;
}
Integer division, not modulus. Modulus operates on integral numbers and we want to keep decimals on the inches.

You must do two logical steps:
1. Convert meters to inches.
2. Separate yards and feet (if any) from the inches. My gek and fobs did show how.
Topic archived. No new replies allowed.