Loop going out of range and decimals.

Hello,
I was trying to do an exercise from the book I'm learning c++ from and below is my code.
The biggest problem is my loop above the commented loop doesn't work properly and goes out of range, and seems like for no reason since it's the "classic way to iterate loops" the book has taught me. I know the one commented out works, but I'd like to use the "classic way" on this exercise, and understanding this problem also would be better for me in the future.

The other problem is that even though I'm using doubles (even long ones), decimals are never shown separated with the decimal point, instead of 12.34 it would store in the vector as 1234.

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
  void drillsChapter4() {
	constexpr double meterToCent = 100.0, inchToCent = 2.54, feetToInch = 12.0;
	std::string unit;
	long double trackSumInMeters = 0.0;
	std::vector<long double> trackInMeters;

	for (bool continueOrNot = 1; continueOrNot == 1; ) {
		long double numberDoub = 0;
		std::cin >> numberDoub >> unit;
		for (; unit != "in" && unit != "inch" && unit != "inches" && unit != "cm" && unit != "centimeter" && unit != "centimeters" && unit != "m" && unit != "meter" && unit != "meters"; ) {
			std::cout << "The unit you entered is not correct, try an available unit. Units available are Inch, Centimeter and Meter.\n";
			std::cin >> unit;
			}

		if (unit == "in" || unit == "inch" || unit == "inches") {
			trackInMeters.push_back((numberDoub*inchToCent) * 100.00);
		}
		else if (unit == "cm" || unit == "centimeter" || unit == "centimeters") {
			trackInMeters.push_back(numberDoub * 100.00);
		}
		else if (unit == "m" || unit == "meter" || unit == "meters") {
			trackInMeters.push_back(numberDoub+0.00);
		}

		std::cout << "\nDo you want to continue? 0 to exit or 1 to continue: ";
		std::cin >> continueOrNot;
	}

	for (int i = 0; 0 < trackInMeters.size(); i++) {
		trackSumInMeters += trackInMeters[i];
	}

	/*for (int x : trackInMeters) {
		trackSumInMeters += x;
	}*/

	std::sort(trackInMeters.begin(), trackInMeters.end());


	for (long double x : trackInMeters) {
		if (x == 0) std::cout << '\n';
		std::cout << x << '\n';
	}

	std::cout << "\nThe sum of all numbers in meters is " << trackSumInMeters << "\nThe number of values entered is " << trackInMeters.size()<< ".\n";
	std::cout << "The largest number is " << trackInMeters[trackInMeters.size() - 1] << " and the smallest is " << trackInMeters[0] << ".\n";
}


I am using Visual Studio 2017 RC.
Thanks in advance.
Last edited on
Line 29, when will this condition be false?
 
    0 < trackInMeters.size()


The other problem is that even though I'm using doubles (even long ones), decimals are never shown separated with the decimal point, instead of 12.34 it would store in the vector as 1234.

That sounds like two different problems. if you have 1234 instead of 12.34 then there must be an error in the calculation somewhere. Another possibly related problem is that if you want to always display 2 decimal places, you would need to specify that, for example
std::cout << std::fixed << std::setprecision(2);
Last edited on
@Chervil

The condition on line 29 is supposed to end before the size of trackInMeter, for example, if there were 3 elements it would read until the second. 3-1=2, basically, it would still read all the numbers it is supposed to. Am I wrong?

And after using your line of code, now I do have decimals, but if I convert inch to centimeter using my code (12 inches to centimeters) it now displays as 3048,00 instead of the correct value 30.48. And I'm sure my constants are correct, I'm doing the calculations on a calculator and they match, but for some reason the computer always displays this wrong.

Really thankful for the help! Thank you.
Last edited on
Well, here it is again,
original:
 
    0 < trackInMeters.size()

corrected:
 
    i < trackInMeters.size()

See it now?


As for the other problem, it does seem that there is something wrong in the calculation.

First, there are constants defined, but never used: meterToCent and feetToInch.
Also at line 16
 
    trackInMeters.push_back((numberDoub*inchToCent) * 100.00);

first the inches are converted to centimetres numberDoub*inchToCent. To convert that to metres. you need to divide by 100 (or by the conversion factor meterToCent ) but instead, the value is multiplied, to give the result in tenths of a millimetre.

Try this:
 
    trackInMeters.push_back((numberDoub*inchToCent) / meterToCent);


Last edited on
Damn, so many errors in "ordinary" basic code, ahahah.

It seems like I need to open my eyes a little bit more, can't see such obvious stuff and that's a big problem if I really what continue dedicating myself to this.

I'm really grateful, thank you for pointing out my stupid errors.
It happens to all of us at times. Sometimes I put extra cout messages for debugging, or use a debugger to trace through the code step by step. Even the most trivial errors can seem invisible occasionally, unless there is some extra information to help pin it down.
Topic archived. No new replies allowed.