1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: First C++ Assignment - Very BASIC

  1. Sep 1, 2009 #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.

    1. The problem statement, all variables and given/known data

    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.

    3. 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?
  2. jcsd
  3. Sep 1, 2009 #2


    User Avatar
    Science Advisor
    Homework Helper

    I would say, don't convert to an integer. That might truncate. Hint: Look in your text book index, or on-line, for printf and precision.
  4. Sep 1, 2009 #3
    printf and precision are not part of the first 2 chapters of the book, nor from the lectures.
  5. Sep 1, 2009 #4


    User Avatar
    Science Advisor
    Homework Helper

    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++).
  6. Sep 1, 2009 #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...
  7. Sep 1, 2009 #6


    Staff: Mentor

    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?
  8. Sep 1, 2009 #7
    I thought about using strings instead of the values, but how would I strip the extra characters off?
  9. Sep 1, 2009 #8


    User Avatar
    Science Advisor
    Homework Helper

    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.
  10. Sep 1, 2009 #9


    User Avatar
    Science Advisor

    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 (Text):

    #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: Sep 1, 2009
  11. Sep 1, 2009 #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.
  12. Sep 2, 2009 #11


    Staff: Mentor

    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.
  13. Sep 2, 2009 #12


    Staff: Mentor

    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 (Text):
    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.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook