|
-
May 24th, 2013, 03:01 PM
#31
Re: Area and volume of cylinder
In main, r and h are defined as integers. cin >> r tries to get an integer and reads 3. cin >> h tries to read another integer but finds . which isn't a digit so the cin fails. Hence h hasn't been changed by the cin. As r and h haven't been previously initialised, h contains whatever happens to be in the memory location used for h. Hence the weid result.
You should check that cin has succeeded after it's used and if it has failed, output an error message. Also r and h should be initialised (usually to 0) when they are defined.
This leads on from my question in post #50 about how are you going to allow the user to input of either int, float or double?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|