# Converting from Meters to Feet in C++

Page 1 of 1

## 4 Replies - 15522 Views - Last Post: 06 May 2009 - 01:47 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=103724&amp;s=57d38fd907ac950311fcbf7320ee8ba5&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 lxlpxp

Reputation: 0
• Posts: 16
• Joined: 01-May 09

# Converting from Meters to Feet in C++

Posted 06 May 2009 - 11:53 AM

// This program converts metric length to English units.

```#include <iostream>
#include <cmath>

using namespace std;

//explains program to user and directs them to enter appropriate values.
void input(double& meters);

//makes converson from metric to English units
void convert(int& feet, double& inches, double meters);

//it will make use of the following global constants
const double METERS_PER_FOOT = 0.3048;
const double INCHES_PER_FOOT = 12.0;

//handles the output as shown in the sample script below
void output(int feet, double inches, double meters);

int main()
{
int feet;
double meters, inches;
char choice;
do
{

input(meters);
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);

convert(feet, inches, meters);
output(feet, inches, meters);

cout << endl;
cout << "Y/y continues, any other quits" << endl;
cin >> choice;
cout << endl;

} while(( choice == 'y') || (choice == 'Y'));

return 0;
}

void input(double& meters)
{
cout << "Enter a number as a double" << endl;
cin >> meters;
}

void convert(int& feet, double& inches, double meters)
{

feet = (meters)/METERS_PER_FOOT;  // this part is giving me the error

}

void output(int feet, double inches, double meters)
{
cout << endl;
cout << "The value of meters, centimeters " << meters << " meters converted to English is " << feet << " feet, " << endl;
}
```

im getting this error

```sprog4.cpp: In function 'void convert(int&, double&, double)':
sprog4.cpp:65: warning: converting to 'int' from 'double'
```

This post has been edited by JackOfAllTrades: 06 May 2009 - 01:54 PM

Is This A Good Question/Topic? 0

## Replies To: Converting from Meters to Feet in C++

### #2 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,233
• Joined: 14-September 07

## Re: Converting from Meters to Feet in C++

Posted 06 May 2009 - 12:22 PM

Two options:

1. change to all doubles
2. type cast there with:

```feet = (int)((meters)/METERS_PER_FOOT); // this part is giving me the error

```

Also use code tags!

### #3 AndyH1963

Reputation: 22
• Posts: 201
• Joined: 22-June 08

## Re: Converting from Meters to Feet in C++

Posted 06 May 2009 - 12:31 PM

You are going to have to cast from double to int. With conversion from a floating point value to an int or long you will also have to round up or down to the nearest real number depending on whether the floating point value is +ve or -ve. You could achieve all this with a simple macro:

```#define round(x) ((x) >= 0 ?(int)((x) +0.5):(int)((x) -0.5))

```

which you place near the top of your source code.

Then use as:

```void convert(int& feet, double& inches, double meters)
{
feet = round((meters)/METERS_PER_FOOT);
}

```

This post has been edited by AndyH1963: 06 May 2009 - 12:33 PM

### #4 lxlpxp

Reputation: 0
• Posts: 16
• Joined: 01-May 09

## Re: Converting from Meters to Feet in C++

Posted 06 May 2009 - 01:46 PM

AndyH1963, on 6 May, 2009 - 11:31 AM, said:

You are going to have to cast from double to int. With conversion from a floating point value to an int or long you will also have to round up or down to the nearest real number depending on whether the floating point value is +ve or -ve. You could achieve all this with a simple macro:

```#define round(x) ((x) >= 0 ?(int)((x) +0.5):(int)((x) -0.5))

```

which you place near the top of your source code.

Then use as:

```void convert(int& feet, double& inches, double meters)
{
feet = round((meters)/METERS_PER_FOOT);
}

```

i got the conversion to work now i need to get the remainding inches im confused on how that works..do i use a static_cast<int>(???) can u help out?

```// This program converts metric length to English units.

#include <iostream>
#include <cmath>

using namespace std;

//explains program to user and directs them to enter appropriate values.
void input(double& meters);

//makes converson from metric to English units
void convert(int& feet, double& inches, double meters);

//it will make use of the following global constants
const double METERS_PER_FOOT = 0.3048;
const double INCHES_PER_FOOT = 12.0;

//handles the output as shown in the sample script below
void output(int feet, double inches, double meters);

int main()
{
int feet;
double meters, inches;
char choice;
do
{

input(meters);
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);

convert(feet, inches, meters);
output(feet, inches, meters);

cout << endl;
cout << "Y/y continues, any other quits" << endl;
cin >> choice;
cout << endl;

} while(( choice == 'y') || (choice == 'Y'));

return 0;
}

void input(double& meters)
{
cout << endl;
cout << "Enter a number as a double" << endl;
cin >> meters;
}

void convert(int& feet, double& inches, double meters)
{

feet = meters/METERS_PER_FOOT;
inches =      // how do i get the remainding part in here?
}

void output(int feet, double inches, double meters)
{
cout << endl;
cout << "The value of meters, centimeters " << meters << " meters converted to English is " << feet << " feet, " <<
inches << " inches" <<  endl;
}
```

*** MOD EDIT: Again...CODE TAGS!!! ***

### #5 janotte

• code > sword

Reputation: 991
• Posts: 5,141
• Joined: 28-September 06

## Re: Converting from Meters to Feet in C++

Posted 06 May 2009 - 01:47 PM