Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,121 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,758 people online right now. Registration is fast and FREE... Join Now!




Conversions

 
Reply to this topicStart new topic

Conversions

RaCheer
4 Mar, 2007 - 12:49 PM
Post #1

New D.I.C Head
*

Joined: 4 Mar, 2007
Posts: 15


My Contributions
I am writing a C++ program to convert feet and inches to meters and centimeters and vice versa. I am having problems figuring out the conversions. I am actually supposed to use reference parameters but I want to make sure I get the conversions working first. What I have so far actually works...it gives me the meters and centimeters, but it gives it to me like this ex: 1.123. I need it to say maybe "30 meters and .123 centimeters. It is also o.k. for the user to enter 6 feet and 37 inches. The program shoulf be able to use this. I can't figure out how to do this though because I will actually need feet and meters to be ints and inches and centimeters to be doubles. I seem to always get stuck on the mindless math conversions. I can do the main bulk of the program myself, but search all over for math conversions. I feel like I'm watsing my time. Will I need to add more functions in order to do this or am I missing something simple? Any help would be greatly appreciated.

CODE


/* --------------------------------------------
Conversions.cpp
Rachel Pope
COP2334
2/27/07

This is a program that allows the user to select their choice of conversion from a menu.
The program uses functions to perform these conversions and supply the user with a
result. The menu is then re-displayed after each conversion is performed.
--------------------------------------------*/

#include <iostream>
#include <cmath>
using namespace std;

int getChoice();
void feet2Meters();
void meters2Feet();





void main()
{
    int choice;
    do{
        choice = getChoice();
        if( choice == 1 ) feet2Meters();
        else if( choice == 2 ) meters2Feet();
        if( choice > 2 || choice < 1 ) cout << "ERROR: Please enter valid choice (1 or 2)." << endl;
    } while( choice != 3 );
    
    
}


//This function displays the calculation menu.

int getChoice()
{
    int c;
    cout << "----------------------\n";
    cout << "1 - Feet and inches into meters and centimeters\n";
    cout << "2 - Meters and centimeters into feet and inches\n";
    cout << "3 - Quit Program\n";
    cout << "----------------------\n";
    cout << "Enter number of choice --> ";
    cin >> c;
    return c;
}


//This function converts feet and inches into meters and centimeters.

void feet2Meters()
{
    double answer1 = 0;
    double meters = 0;
    double feet = 0;
    double inches = 0;
    cout << "Enter feet.\n";
    cin >> feet;
    cout << "Enter inches.\n";
    cin >> inches;
    answer1 = (feet*12);
    inches = (answer1 + inches);
    meters = inches/39.37;
    cout << "There are this many " << meters << " meters and centimeters." << endl;
    
    
    
    
}

//This function converts meters and centimeters into feet and inches.

void meters2Feet()
{
    cout << "This is where I will do my math.";

}



User is offlineProfile CardPM
+Quote Post

k0b13r
RE: Conversions
4 Mar, 2007 - 02:07 PM
Post #2

D.I.C Head
Group Icon

Joined: 18 Jul, 2006
Posts: 196



Thanked: 1 times
Dream Kudos: 250
My Contributions
Don't use <code> & </code>, use [ ] (instead of < >) biggrin.gif to show us your code smile.gif

This post has been edited by k0b13r: 4 Mar, 2007 - 02:08 PM
User is offlineProfile CardPM
+Quote Post

RaCheer
RE: Conversions
4 Mar, 2007 - 02:30 PM
Post #3

New D.I.C Head
*

Joined: 4 Mar, 2007
Posts: 15


My Contributions
Sorry! I'm used to HTML...lol. I have gone ahead and added some extra functions as well as reference parameters. I also failed to mention that there are 0.3078 meters/foot and 100 cm/meter. Here is my edited code and question I asked to begin with still stands. Thanks so much!

CODE



/* --------------------------------------------
Conversions.cpp
Rachel Pope
COP2334
2/27/07

This is a program that allows the user to select their choice of conversion from a menu.
The program uses functions to perform these conversions and supply the user with a
result. The menu is then re-displayed after each conversion is performed.
--------------------------------------------*/

#include <iostream>
#include <cmath>
using namespace std;

int getChoice();
void feet2MetersInput(double& feet, double& inches, double& meters);
void feet2Meters (double& feet, double& inches, double& meters);
void meters2FeetInput(double& meters, double& centimeters, double& feet);
void meters2Feet(double& meters, double& centimeters, double& feet);





void main()
{
    double f, i, m, c;
    int choice;
    do{
        choice = getChoice();
        if( choice == 1 ) feet2MetersInput(f, i, m);
        else if( choice == 2 ) meters2FeetInput(m, c, f);
        if( choice > 2 || choice < 1 ) cout << "ERROR: Please enter valid choice (1 or 2)." << endl;
    } while( choice != 3 );
    
    
}


//This function displays the calculation menu.

int getChoice()
{
    int c;
    cout << "----------------------\n";
    cout << "1 - Feet and inches into meters and centimeters\n";
    cout << "2 - Meters and centimeters into feet and inches\n";
    cout << "3 - Quit Program\n";
    cout << "----------------------\n";
    cout << "Enter number of choice --> ";
    cin >> c;
    return c;
}


//This function allows the user to input their measurements for conversion.

void feet2MetersInput(double& feet, double& inches, double& meters)
{
    cout << "Enter feet.\n";
    cin >> feet;
    cout << "Enter inches.\n";
    cin >> inches;
    feet2Meters(feet, inches, meters);
    
    
}

//This function converts feet and inches into meters and centimeters.

void feet2Meters(double& feet, double& inches, double& meters)
{
    double answer1 = 0;
    answer1 = (feet*12);
    inches = (answer1 + inches);
    meters = inches/39.37;
    cout << "There are this many " << meters << " meters and centimeters." << endl;
    
    
    
    
}

//This function allows the user to enter their measurements for conversion.

void meters2FeetInput(double& meters, double& centimeters, double& feet)
{
    cout << "Enter meters.\n";
    cin >> meters;
    cout << "Enter centimeters.\n";
    cin >> centimeters;
    meters2Feet(meters, centimeters, feet);
    
}


//This function converts meters and centimeters into feet and inches.

void meters2Feet(double& meters, double& centimeters, double& feet)
{
    double answer2 = 0;
    answer2 = meters/100;
    centimeters = (answer2 + centimeters);
    feet = centimeters/30.48;
    cout << "There are this many " << feet << " feet and inches." << endl;
}



User is offlineProfile CardPM
+Quote Post

Wild_Rose
RE: Conversions
13 Apr, 2007 - 04:17 AM
Post #4

New D.I.C Head
*

Joined: 13 Apr, 2007
Posts: 39


My Contributions
you can also use switch() to do that right?
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Conversions
13 Apr, 2007 - 06:46 AM
Post #5

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
The switch statement would work just as well
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 10:09PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month