Need help with very basic C++

Started by
1 comment, last by Theo Berlin 10 years, 9 months ago

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! tongue.png

#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

Advertisement

1. You instantiate a std::vector inside both the while() loop,and your unit != "none" if-test

which means that the object std::vector values is dead after the innermost SCOPE that contains your vector declaration ends

a scope is typically whatever has {} brackets around it, and so anything inside any scope lives until the scope ends, except this is C++, so apply bag of salt (heap allocated memory, ...)

the solution is to put it outside the while() loop, so that you can continue to accumulate values over several loops, which I think is what you want

2. looping through the elements of a vector with int is quite ok, however, size() is of type size_t, so the correct solution is:

for (size_t i = 0; i < vector.size(); i++)

{

ffff

}

otherwise, you can also:

for (double value : vector)

sum += value;

Also, don't stop using SL (standard library).. It works great, and has most of what you need.

I have a 35k LOC game engine that has uses only my own framework + SL

Of course, there are benefits in using boost, however, perhaps that's for the guys that already know everything, and don't want to take the slightly longer way around

Thanks a lot for helping me out! I finally got the program working exactly the way I want it to. (to get out of the while loop I use ctrl+z).

I'm posting it here in case anyone i who reads this book gets stuck on the same drill and finds this page in the future tongue.png


#include "../../std_lib_facilities.h"

int main()

{
	double d = 0;
	double smallest = 0;
	double largest = 0;
	string unit;
	vector <double> values;
	double sum = 0;
	int first_loop = 0;
		while (cin >> d && cin >> unit){
			if (first_loop == 0){
				smallest = d;
				largest = d;}
			
				if (unit == "cm"){
					cout << d << unit << " == " << d/100 << "m\n";
					values.push_back(d/100);}
				if (unit == "m"){
					cout << d << unit << " == " << d << "m ... duh...\n";
					values.push_back(d);}
				if (unit == "in"){
					cout << d << unit << " == " << d*0.0254 << "m\n";
					values.push_back(d*0.0254);}
				if (unit == "ft"){
					cout << d << unit << " == " << d*0.3048 << "m\n";
					values.push_back(d*0.3048);}

				if (first_loop == 1){
		if (d > largest) 
			largest = d;
		if (d < smallest)
			smallest = d;
		sort(values.begin(),values.end());}
			first_loop = 1;
		}
			cout << "the values you entered are:\n";
			for (size_t i = 0; i < values.size(); ++i){
			sum += values[i];
			cout << values[i] << "m\n";
			}
		cout << "you entered " << values.size() << " values\nthe sum of these values is " << sum<< "m\n";
		cout << "the largest value is " << largest << '\n';
		cout << "the smallest value is " << smallest << '\n';
		keep_window_open();
}

This topic is closed to new replies.

Advertisement