Comp Sci How to Handle Decimal Precision in C++ for BMI Calculations?

  • Thread starter Thread starter exitwound
  • Start date Start date
  • Tags Tags
    Assignment C++
AI Thread Summary
The discussion focuses on handling decimal precision in C++ for BMI calculations, particularly for a homework assignment requiring height and weight conversions. Participants express challenges with formatting outputs to meet specific requirements, such as displaying metric weights with one decimal place and pounds as whole numbers. Suggestions include using `cout.precision` and `fixed` for proper formatting, despite these topics not being covered in class yet. There is also a debate about whether to convert floating-point numbers to integers, which could lead to truncation issues. Ultimately, the consensus is to explore formatting options to achieve the desired output while adhering to assignment guidelines.
exitwound
Messages
291
Reaction score
1
This is our very first assignment in C++, from the very first week of class so we don't know a whole lot yet. Keep that in mind.

Homework Statement



Write a program that will:
-input someone's height in feet and inches
-output that person's height in centimeters
-output the weight in kilograms and pounds for a BMI of 18.5
-output the weight in kilograms and pounds for a BMI of 25.0

BONUS (5 points)
Include exactly one decimal places in all metric outputs and no decimal places on the values in pounds.

The Attempt at a Solution



The program is done and finished. However, I'm having a little trouble with the bonus question.

We're talking about weights here, so precision isn't required beyond maybe 1 decimal point. He specifically asks for the weights in pounds to be without decimal points so I've converted the floating point variable to an integer, which drops the tenths and just displays the whole portion of the number. I get a warning of 'potential loss of data' from the compiler, but I know that.

We were given that 1in=2.54cm and that 1lb=2.2kg in the instructions to the problem. Now, I assume that he wants us to use those numbers intact in the code to calculate the final answer, and then do something to display them with only 1 decimal digit. However, I can't figure out how to do that. From a google search, I know there's a cout.precision but we haven't talked about it in class and it's not in the first 2 chapters of the book so I'm afraid that's beyond what he's referring to using.

Any thoughts?
 
Physics news on Phys.org
I would say, don't convert to an integer. That might truncate. Hint: Look in your textbook index, or on-line, for printf and precision.
 
printf and precision are not part of the first 2 chapters of the book, nor from the lectures.
 
Maybe he would give bonus points to individuals who look ahead in the book (?). Alternately, you might be able to use cout.precision, like you mentioned (?).

-----
"C makes it easy to shoot yourself in the foot. C++ makes it harder, but when you do, you blow your whole leg off."[/color] --Bjarne Stroustrup (inventor of C++).[/color]
 
I actually emailed him about looking outside the scope of the taught material and he said all the information needed for the bonus was included in the class lectures and the assigned chapters :\

I just read thru the 2 chapters again and it only covers the absolute very basics. cin, cout, data types, comments...
 
You might convert the metric height and the two metric weights to strings, and then strip off any characters from the 2nd decimal place on, then print the strings.

Also, have you checked the notes you took in class?
 
I thought about using strings instead of the values, but how would I strip the extra characters off?
 
exitwound: OK, so if you're sure you can't use cout.precision, then perhaps create a calculation such that when you convert the result to an integer, it turns out to be the same answer as if you rounded it, instead of truncating.
 
exitwound said:
I actually emailed him about looking outside the scope of the taught material and he said all the information needed for the bonus was included in the class lectures and the assigned chapters :\

I just read thru the 2 chapters again and it only covers the absolute very basics. cin, cout, data types, comments...

Erk. What is the book, by the way?

If you are learning programming, then the appropriate way to do this is with some kind of formatted output. In your place, I would go ahead and try using cout.precision, whether it is in the chapter or not. He can hardly mark you down for using cout properly to get the required effect, and it is not all about marks anyway. You are doing yourself a favour for learning the right way to do it from the start. In my view, reading ahead of the lectures or from other sources is a perfectly normal and sensible thing for a student to do.

Cheers -- sylas

Postscript. In addition, you need to be careful about whether you are using "fixed" as the format for floating point numbers. If you print the number 3.20 to two decimal places, you get "3.2" by default, and "3.20" in the fixed style.

Try these statements.
Code:
#include <iostream.h>
#include <iomanip.h>
using namespace std;

void main()
{
    double x = 3.198;

    cout << x << endl;
    cout << setprecision(2) << x << endl;
    cout << fixed << setprecision(2) << x << endl;
}

(I don't have a working c++ compiler handy. I usually use C so I'm taking a risk giving this code. Sorry. If this has an error, tell me!)
 
Last edited:
  • #10
Indeed. I enjoy just pulling out the book and reading through it. I got me a copy of Visual Studio 2008 thanks to the Microsoft Academic Alliance. It's what we're using in class.

The book assigned to the class is "Starting out with C++ - From Control Structures through Objects - 6th Ed" by Tony Gaddis. Teacher says it's a good one, but any C++ book will do, really. He doesn't teach out of the book, but it's a guide with lots of practice problems for us. Plus it reads really easily.

I think if I don't figure it out by Monday, when it's due, I'll slap in the precision and see what he says. I can't be docked points for it so might as well give it a shot.
 
  • #11
exitwound said:
The book assigned to the class is "Starting out with C++ - From Control Structures through Objects - 6th Ed" by Tony Gaddis. Teacher says it's a good one, but any C++ book will do, really. He doesn't teach out of the book, but it's a guide with lots of practice problems for us. Plus it reads really easily.
Interesting. I have what is probably the first edition of this book, Starting out with C++. I received a free copy because I was one of the reviewers.
 
  • #12
exitwound said:
I thought about using strings instead of the values, but how would I strip the extra characters off?
You would have to have some logic to determine how many digits are to the left of the decimal point, such as in the following code.
Code:
if (metric_ht >= 10.0 && metric_ht < 100.0)
// Two digits to the left of the decimal point 
// Convert to string and keep first three digits
else if (metric_ht >= 100.0 && metric_ht < 1000.0)
// Three digits to the left of the decimal point
// Convert to string and keep first four digits
After you know how many digits are to the left of the decimal point and have converted it to a string, you can pull off the first however many characters you're interested in using a for loop, remembering to display a decimal point in the appropriate position.
 

Similar threads

Replies
12
Views
2K
Replies
8
Views
2K
Replies
23
Views
3K
Replies
5
Views
2K
Replies
1
Views
7K
Replies
19
Views
3K
Replies
3
Views
2K
Replies
6
Views
4K
Replies
15
Views
2K
Back
Top