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.
void drillsChapter4() {
constexprdouble meterToCent = 100.0, inchToCent = 2.54, feetToInch = 12.0;
std::string unit;
longdouble trackSumInMeters = 0.0;
std::vector<longdouble> trackInMeters;
for (bool continueOrNot = 1; continueOrNot == 1; ) {
longdouble 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);
}
elseif (unit == "cm" || unit == "centimeter" || unit == "centimeters") {
trackInMeters.push_back(numberDoub * 100.00);
}
elseif (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 (longdouble 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.
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);
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.
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.
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.