Converting from Meters to Feet in C++

Converting from Meters to Feet

Page 1 of 1

4 Replies - 15522 Views - Last Post: 06 May 2009 - 01:47 PM Rate Topic: -----

#1 lxlpxp   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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'


*** MOD EDIT: Added code tags. Please :code: ***

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   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • 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! :angry:
Was This Post Helpful? 0
  • +
  • -

#3 AndyH1963   User is offline

  • D.I.C Head
  • member icon

Reputation: 22
  • View blog
  • 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);
}



Was that helpful or not?

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

Was This Post Helpful? 0
  • +
  • -

#4 lxlpxp   User is offline

  • New D.I.C Head

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

Re: Converting from Meters to Feet in C++

Posted 06 May 2009 - 01:46 PM

View PostAndyH1963, 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);
}



Was that helpful or not?


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!!! ***
Was This Post Helpful? 0
  • +
  • -

#5 janotte   User is offline

  • code > sword
  • member icon

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

Re: Converting from Meters to Feet in C++

Posted 06 May 2009 - 01:47 PM

Please edit your posting (see the "EDIT" button in lower right).
( a ) Delete all your code.
( b ) Get a fresh copy of the code with formatting in place from your editor / IDE.
( c ) Paste the formatted code between code tags like this :code:
( d ) Use the "Preview Post" button to check it's all good.
( e ) Use the "Submit Modified Post" button to finish the editing.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1