My bad, I was stating that the first number entered should be the greatest and least value because it is the only one. My program, however, was only outputting that it was the least value so far. When I deleted the "else" from the "else if" statement, it fixed the problem.
I have struck another problem, unfortunately. I have gotten my program to allow input of numbers and units: centimeters, meters, feet, inches. Next, I convert every number to meters and also state the least and greatest number so far, along with the sum after I enter each number. The problem is, is that I now need to create a vector that holds all the converted numbers in order to find the greatest value, least value, number of elements in the vector and to print out all those elements. I know how to find all of this once I create the vector, but I am unsure on where to put it. Hints in the right direction would be amazing! thank you!
here's my code, once again feel free to run it!
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
|
#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
cout << "Enter some numbers: " << endl;
double num = 0;
const double cm = 100;
const double in = 39.3701;
const double ft = 3.28084;
const double m = 1;
string unit = "";
double smallest = 1.0/0.0; //setting smallest to infinity
double largest = -1.0/0.0; //setting largest to -infinity
double sum = 0;
while (cin>>num>>unit){ //while you take in user input
if (unit == " "){ //if number does not have unit, but " "
double meter = num;
cout<<num<<" = "<<num<<"m"<<endl;
num = meter;
sum+=num; //add converted number to sum
}
if (unit == "cm"){ //centimeter
double meter = num/cm; //convert cm to m
cout<<num<<"cm = "<<meter<<"m"<<endl;
num = meter; //num equals converted number
sum+=num; //add to sum
}
else if (unit == "in"){ //inch
double meter = num/in; //convert in to m
cout<<num<<"in = "<<meter<<"m"<<endl;
num = meter; //num equals convert number
sum+=num; //add to sum
}
else if (unit == "ft"){ //feet
double meter = num/ft; //convert to m
cout<<num<<"ft = "<<meter<<"m"<<endl;
num = meter; //num equals converted num
sum+=num; //add to sum
}
else if (unit == "m"){ //meter
double meter = num/m; //convert meter to meter
cout<<num<<"m = "<<meter<<"m"<<endl;
num = meter;
sum+=num; //add to sum
}
else{
cout<<"Not a valid unit."<<endl;
}
cout<<"Sum: "<<sum<<endl; //print out sum
if (num == smallest || num < smallest){
smallest = num;
cout<<num<<" is smallest so far."<<endl;
}
if(num>largest){
largest = num;
cout<<num<<" is largest so far."<<endl;
}[code]
|
}
vector<double>numbers; //vector to hold converted numbers
for(double meter;cin>>meter;){
numbers.push_back(meter);
}
cout<<"Number of numbers: "<<numbers.size()<<"\n"; //size
}
[/code]