Write a C++ program to convert a distance given in feet and inches to meters.

Hey guys!
My instructor gave us an assignment to Write a C++ program to convert a distance given in feet and inches to meters. The problem is that he hasn't showed us how to do this using feet and inches. Ive been looking for tutorials to teach me how to write this program but I've come up empty. Does anyone know how to write this and does anyone have any c++ sites they recommend for beginners? Thanks n advance for any and all help guys!


He wants us to use these conversions for the problem.
Conversion ratios: one foot is 12 inches, one inch is 2.54 centimeters and one meter is 100 centimeters.
This is what I attempted in visual studio code.

//
// conv.cpp
// Apple Honey
// Jun. 22,2020
//
// CSCI-14 unit conversion program
// convert a distance given in feet and inches to meters
// one foot is 12 inches, one inch is 2.54 centmeters,
// and one meter is 100 centimeters.
//

#include<iostream.h>
#inclue <iomanip>
using namespace std;
int main()
{
const float CENTIMETERSPERINCH = 2.54;
cont int INCHESPERFOOT = 12;
const int CENTIMETERSPERMETER = 100;
int feet = 0;
int inches = 0;

std::cout << "converting feet/inches to meters\n";
std::cout << "enter feet: ";
std::cin >> feet;
std::cout << "enter inches: ";
std::cin >> inches;

int inchesoffeet = 0;

if (feet >= 0 && inches >= 0)
{
for (int i = 0; i < feet; i++)
{
inches += INCHESPERFOOT;
}
}
else
{
std::cout<< " Try again\n";
return 0;
}
float finalmeters = (inches + inchesoffeet) * CENTIMETERSPERINCH;
std::cout << feet << "feet, " << inches << " inches = " << finalmeters<< " meters\n";
return 0;
}

Last edited on
What is the user supposed to enter?
Can you write a program to convert just inches to centimeters?
Try that first, and then add more to that.

<OP added code after my post>
Last edited on
my program will prompt for feet (a whole number only) and inches (allow a fractional part). It
will then convert the distance to meters and display the user’s entries and the result with
reasonable descriptive text. For example, a test run could look like this:

Enter feet : 0
Enter inches : 39.3701
0 feet and 39.3701 inches is 1 meters.
l know how to write a program for feet and inches to cm so ill try that step first :)
When you calculate inches, why not just do:

int total_inches = feet * 12 + inches;

Then do
double centimeters = total_inches * 2.54;

Then do
1
2
double meters = centimeters * 100;
double meters = centimeters / 100; //see Furry Guy's post :) 


It might do the same thing as what you're already doing, but avoids a loop like you have with for (int i = 0; i < feet; i++).
Last edited on
Pssst, Ganado.....

I think you meant double meters = centimeters / 100;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<iostream>
using namespace std;

int main()
{

int feet = 0;
int inches = 0;

std::cout << "converting feet/inches to meters\n";
std::cout << "enter feet: ";
std::cin >> feet;
std::cout << "enter inches: ";
std::cin >> inches;

int total = feet * 12 + inches;
double result = total * .0254;
cout << endl;

cout << feet << "\'" << inches << "\"" << " equals " << result << " meters.";
return 0;
}
Went out of my way to over complicate things BUT i did post the shorter code afterwards. Just wanted to show that you can approach problems from multiple angles. I over complicate things all the time, it's a habit. I just love using loops.

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

int main(){

  int distFeet = 0;
  int distInch = 0;

  std::cout << "What is the distance? (feet inches): ";
  std::cin >> distFeet >> distInch;

  //if user enters more than 12 inches, convert inches to feet.
  if(distInch >= 12){
    ++distFeet;
    distInch -= 12;
  }

  //one foot is 12 inches
  //one inch is 2.54 centimeters
  //one meter is 100  centimeters

  int inches = distInch;
  //convert inches from feet
  for(int i = 0; i < distFeet; i++){
    inches += 12;
  }

  double centimeters = 0;

  //convert centimeters from inches
  for(int i = 0; i < inches; i++){
    centimeters += 2.54;
  }

  int meter = 0;
  //convert centimeters to meters;
  while(centimeters > 100){
    meter++;
    centimeters -= 100;
  }

  std::cout << distFeet << " ft. and " << distInch << " inches equals:\n"
            << meter << " meters, " << centimeters << " centimeters" << std::endl;

  return 0;
}


Here is the shorter version:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

int main(){

  int feet, inches;

  std::cout << "Enter Distance to convert (ft inches) ";
  std::cin >> feet >> inches;

  double total = (feet * 12) + inches;
  total *= 2.54 / 100;


  std::cout << total << " meters" << std::endl;

}
Topic archived. No new replies allowed.