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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
#include<iostream>
using namespace std;
// Constant factor converting feet to meters: 3.28 feet = 1 meter
const double FEET_TO_METERS_FACTOR = 0.304878048; // (1 / 3.28)
// TODO: Declare 4 function prototypes
// - Name: getFeet
// Parameters: <none>
// Returns: an int for the number of feet entered by the user
int getFeet();
// - Name: getInches
// Parameters: <none>
// Returns: a double for the number of inches entered by the user
double getInches();
// - Name: convertToMeters
// Parameters:
// - 1 int for feet (pass by value)
// - 1 double for inches (pass by value)
// Returns: a double for the number of meters
double convertToMeters(int feet, double inches);
// - Name: displayResults
// Parameters:
// - 1 int for feet (pass by value)
// - 1 double for inches (pass by value)
// - 1 double for meters (pass by value)
// Returns: <nothing>
void displayResults(int feet, double inches, double meters);
// ********************************************
int main()
{
// Declare program variables.
int feet;
double inches, meters;
// TODO: Call function to get feet input.
// An int value is returned - assign to the correct variable
cin >> feet;
int getFeet(feet);
// TODO: Call function to get inches input.
// A double value is returned - assign to the correct variable
cin >> inches;
double getInches(inches);
// TODO: Call function to convert feet and inches to meters.
// Pass the two arguments to the function
// Assign the returned value to the correct variable
convertToMeters(feet, inches);
// TODO: Call function to print the results.
// Pass the correct three arguments to the function
// Nothing is returned, so do NOT provide a variable assignment
displayResults(feet, inches, meters);
cout << "End program - ";
return 0;
}
// ********************************************
// TODO: Define getFeet()
// Refer to the function prototype above for parameter and return type info
//
// Repeat
// - Prompt the user to enter feet
// - Read the user's keyboard response
// Until a value of 0 or more is entered
//
// Note: You will need to declare a local variable inside the function to hold
// the user's entry for feet (be sure to use correct data type)
int getFeet()
{
int feet;
cin >> feet;
while (feet <= 0)
{
cout << "Enter feet: ";
cin >> feet;
}
return feet;
}
// TODO: Define getInches()
// Refer to the function prototype above for parameter and return type info
//
// Repeat
// - Prompt the user to enter inches
// - Read the user's keyboard response
// Until a value of 0 or more and less than 12 is entered
//
// Note: You will need to declare a local variable inside the function to hold
// the user's entry for inches (be sure to use correct data type)
double getInches()
{
double inches;
cin >> inches;
while (inches > 0 && inches < 12)
{
cout << "Enter inches: ";
cin >> inches;
}
return inches;
}
// ********************************************
// TODO: Define convertToMeters()
// Refer to the function prototype above for parameter and return type info
//
// - Declare a new local variable called fractionalFeet that is type double
// - Type cast feet to be a double and assign to the fractionalFeet variable
// - On the same line where you are assigning the converted feet value to
// - fractionalFeet, also convert inches to feet (inches / 12) and add to the feet
// - Finally, return the result of multiplying fractionalFeet by FEET_TO_METERS_FACTOR
double convertToMeters(int feet, double inches)
{
double fractionalFeet;
double cast_feet(fractionalFeet);
double(fractionalFeet * FEET_TO_METERS_FACTOR);
return fractionalFeet;
}
// ********************************************
// TOTO: Define displayResults()
// Output statement to format results as shown
// using values passed to parameters:
// e.g.
//
// 10'-6.25" = 3.20757 meter(s).
//
// Use string literals along with value parameters:
// "'"
// "\" = "
// " meter(s)."
void displayResults(int feet, double inches, double meters)
{
cout << endl;
cout << feet << "'-" << inches << "\" = " << meters << " meter(s)." << endl;
}
|