Skip to content

Instantly share code, notes, and snippets.

@Dominent
Created April 23, 2015 08:44
Show Gist options
  • Save Dominent/50496a98914d2a0672d5 to your computer and use it in GitHub Desktop.
Save Dominent/50496a98914d2a0672d5 to your computer and use it in GitHub Desktop.
/*Write a program that converts inches, centimeters, feet to meters,
writes out the smallest value, the largest, the number of values entered
and the sum of all values entered*/
#include <iostream>
#include <string>
#include <cstdlib>
#include "custom_functions.h"
#include <vector>
#include <algorithm>
using namespace std;
int main(){
double convert_meters = 0; //values entered converted in meters
double current_val = 0;
double previous_val = 0;
double large_val = 0;
double small_val = 1000;
string unit = "???";
double sum;
int i = 0;
vector<double>input_values;
cout << "\nEnter a variable followed by its unit!"
<< "\nAccepted units are [cm], [m], [in], [ft]"
<< "\nTo exit enter '-1' "
<< '\n';
for (int x = 0; current_val != -1; ++x){
cin >> current_val >> unit;
if (unit == "cm"||unit == "CM"||unit == "Cm"||unit == "cM") convert_meters = current_val * 0.01;
else if (unit == "m" || unit == "M") convert_meters = current_val;
else if (unit == "in" || unit == "IN" || unit == "In" || unit == "iN") convert_meters = current_val * 0.0254;
else if (unit == "ft" || unit == "FT" || unit == "Ft" || unit == "fT") convert_meters = current_val * 0.3048;
else{
cout << "Bad Input" << '\n';
break;
}
input_values.push_back(convert_meters);
sum = previous_val + convert_meters;
++i;
cout << convert_meters;
if (convert_meters > large_val){
large_val = convert_meters;
cout << "largest so far" << '\n';
}
else if (convert_meters < small_val){
small_val = convert_meters;
cout << "The smallest so far" << '\n';
}
else cout << "The entered number is the same as the previous" << '\n';
previous_val = convert_meters;
} // end of for() loop
sort(input_values.begin(), input_values.end());
for (int x = 0; x < input_values.size(); ++x){
cout << input_values[x] << '\t';
}
cout << "\nThe smallest value is:" << small_val << "[m]"
<< "\nThe largest value is:" << large_val << "[m]"
<< "\nThe number of values entered is:" << i
<< "\nThe sum of all values is:" << sum << "[m]"
<< '\n';
stop_clossing();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment