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

In summary: C++ Primer and Quickstart from amazon.com to supplement the class.In summary, the program 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.0In summary, the program 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.0In summary,
  • #1
exitwound
292
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
  • #2
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.
 
  • #3
printf and precision are not part of the first 2 chapters of the book, nor from the lectures.
 
  • #4
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." --Bjarne Stroustrup (inventor of C++).
 
  • #5
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...
 
  • #6
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?
 
  • #7
I thought about using strings instead of the values, but how would I strip the extra characters off?
 
  • #8
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.
 
  • #9
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.
 

1. What is the purpose of the "First C++ Assignment - Very BASIC"?

The purpose of this assignment is to introduce students to the basics of the C++ programming language and to help them develop fundamental programming skills.

2. What is the level of difficulty for this assignment?

This assignment is designed for beginners and is considered to be very basic in terms of difficulty. It is meant to be a starting point for students who are new to programming.

3. Do I need any prior knowledge of programming to complete this assignment?

No, this assignment is meant for beginners with little to no prior knowledge of programming. It is designed to be a first assignment for students who are new to C++.

4. How long does it typically take to complete this assignment?

The time it takes to complete this assignment can vary depending on the individual's prior experience and understanding of programming. However, for most beginners, it can take anywhere from a few hours to a day or two.

5. What can I expect to learn from completing this assignment?

By completing this assignment, you can expect to gain a basic understanding of the C++ programming language, including concepts such as variables, data types, control structures, and basic input/output operations. You will also learn how to write simple programs and solve basic programming problems.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Set Theory, Logic, Probability, Statistics
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
4K
  • Programming and Computer Science
Replies
5
Views
2K
  • Introductory Physics Homework Help
Replies
6
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Programming and Computer Science
Replies
2
Views
7K
Back
Top