Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Purpose: This program was designed to allow a user to input a measurement and
- // convert it to it's opposite measurement. In this example, we are
- // converting inches to centimeters and vice versa.
- // Author: Jorge Aragon
- // Date: 12/6/2015
- // Filename: Introductory18.cpp
- // Input: Whether the intial measurement was in Inches or Centimeters and the
- // measurement itself.
- // Output: The conversion from centimeters to inches and vice versa.
- #include <iostream>
- //I am including iomanip as I will need to set the precision of the
- //numbers to two decimal place.
- #include <iomanip>
- using namespace std;
- //Here, I am doing a prototype of the function for both conversions that will happen
- //in the program. For example, conv1 is the conversion from inches to centimeters
- //and conv2 is the conversion from centimeters to inches.
- double conv1(double in);
- double conv2(double cm);
- int main()
- {
- //Here, I am defining my variables for inches, centimeters, the conversion total,
- //the answer to question 1, and the answer to question 2.
- double inches = 0.0;
- double cent = 0.0;
- double conv = 0.0;
- char ques1 = ' ';
- char ques2 = ' ';
- //The program begins by asking the user if he/she will like to put in the initial
- //measurement in inches. It is storing the value as ques1.
- cout << "Is the initial measurement in Inches? (y/n): ";
- cin >> ques1;
- //Here is the first if statment. If the user it going to put in inches, this program
- //will go ahead and use the first conversion: conv1.
- if (ques1 == 'y' || ques1 == 'Y')
- {
- //The line and space below is just for decoration and make the program easier to read.
- cout << "______________________________________________" << endl;
- cout << " " << endl;
- //Here, the user is asked to input the initial measurement in inches and store the value
- //in the inches variable.
- cout << "Type in the measurement in Inches: ";
- cin >> inches;
- //Here, the program is calling to function conv1 to do the conversion.
- conv = conv1(inches);
- //This code below is setting the decimal value point to 2 becuase some answers will be longer
- //than 3 decimals.
- cout << fixed << setprecision(2);
- //Another couple of spaces to make the program easier to understand.
- cout << " " << endl;
- //Below, the program outputs the returned value for converting inches to centimeters.
- cout << inches << " Inches is equal to " << conv << " Centimeters." << endl;
- //More lines and spaces to make the program easier to read.
- cout << " " << endl;
- cout << "______________________________________________" << endl;
- }
- //If the user has an initial measurement in centimeters, the program will then confirm:
- else if (ques1 == 'n' || ques1 == 'N')
- {
- //This space is to make the program easier to read.
- cout << " " << endl;
- //The program is confirming that the user wants to type in the initial measurement in
- //centimeters and saves the answer to ques2.
- cout << "Is the initial measurement in Centimeters? (y/n): ";
- cin >> ques2;
- //Once the program has confirmed that the user is going to type in the initial measurement
- //in centimeters, then it'll continue to use the second conversion:
- if (ques2 == 'y' || ques2 == 'Y')
- {
- //More spaces and lines to make the program easier to read.
- cout << "______________________________________________" << endl;
- cout << " " << endl;
- //Here, the user is asked to type in the initial measurement in Centimeters and saves
- //the answer to cent.
- cout << "Type in the measurement in Centimeters: ";
- cin >> cent;
- //Here, the program is calling to the second conversion (conv2) so that it can convert
- //centimeters to inches.
- conv = conv2(cent);
- //Below, I am telling the program to make numbers accurate to the second decimal place.
- cout << fixed << setprecision(2);
- //Spaces and lines below to make the program easier to read.
- cout << " " << endl;
- //The code below outputs the returned value from conv2 to the user that shows how many
- //inches are in the given amount of centimeters.
- cout << cent << " Centimeters is equal to " << conv << " Inches" << endl;
- //More spaces and lines for formatting.
- cout << " " << endl;
- cout << "______________________________________________" << endl;
- }
- //If the user answered no to both questions, then I assume that they do not want to convert
- //any measurements:
- else if (ques2 == 'n' || ques2 == 'N')
- {
- //The spaces and lines below are for formatting and telling the user to have a nice day.
- cout << " " << endl;
- cout << "You are not converting any measurements, Have a Nice Day." << endl;
- cout << " " << endl;
- }
- //end of main function
- }
- return 0;
- }
- //Here, I am defining the first value-returning function that will take the amount of inches given and
- //convert them to centimeters.
- double conv1(double in)
- {
- //1 Centimeter is exactly .39370 inches. Here, the program devides the inches by .39370 to get the
- //total amount of centimeters and returns this to the main function
- return in/.39370;
- }
- //This is the second value-returning function that will convert centimeters to inches.
- double conv2(double cm)
- {
- //Because 1 Centimeter is equal to .39370, I did the opposite of the algorithm above to get the amount
- //of centimeters in an inch.
- return cm*.39370;
- }
- //end of program.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement