Hello, I started learning C++ a couple days ago. I've got Bjarne Stroustrup's "Programming Principles and Practice Using C++".
I got to the chapter 4 drill, part 10. The program's supposed to read an integer and a string (unit). Then it should convert the string unit to meters and store it in a vector. Then,
"When the loop ends, print the smallest, the largest, the number of values, and the sum of values. Note that to keep the sum, you have to decide on a unit to use for that sum; use meters".
Before you read my code, please know that I'm aware of how bad and laughable it is (and how I should stop using std lib), but one day my programs will conquer the world, so just you wait!
#include
"../../std_lib_facilities.h"
int
main()
{
double d;
double smallest = 0;
double largest = 0;
string unit = "none";
while (cin >> d && cin >> unit){
if (unit != "none"){
double sum = 0;
vector <double> values;
if (unit == "cm"){
values.push_back(d/100);}
if (unit == "m"){
values.push_back(d);}
if (unit == "in"){
values.push_back(d*0.0254);}
if (unit == "ft"){
values.push_back(d*0.3048);}
sort(values.begin(),values.end());
for(int i=0; i<values.size(); i++){ // warning C4018: '<' : signed/unsigned mismatch
sum += values;
cout <<
"\nthe values you entered are: " << values << '\n';
cout <<
"you entered " << values.size() << " values\nthe sum of these values is " << sum<< "m\n";
}
if (d > largest)
largest = d;
cout <<
"the largest value is " << largest << '\n';
if (d < smallest)
smallest = d;
cout <<
"the smallest value is " << smallest << '\n';
?
}
}
}
One of the main problems is that it loops and THEN reads another d into the vector. Also, that error message after the
for(int i=0; i<values.size(); i++){
is confusing me.
Again, laugh at your own peril! *play evil laughing sound*
Thanks!
/Theo