![]() |
|
|||||||
|
|
Thread Tools | Search this Thread | Rate Thread |
|
#1
|
|||
|
|||
Converting numbers using explicit type conversion (casting)Hello everyone, I'm a beginner C++ programmer and I have some doubts concerning the following code.
I need a program that prompts the user to input five decimal number, and then the program has to print these 5 numbers. Then I have to convert each decimal number to the nearest integer. So far in the book, we've only learned about static_cast conversion, so I was trying to use that, but got the following error Quote:
Anyways, here's my code. Thanks in advance to all of you for your help! CPP / C++ / C Code:
|
|||
|
#2
|
|||
|
|||
Re: Converting numbers using explicit type conversion (casting)Quote:
The variable on the left-hand of the equals sign in an assignment statement can't have a cast. In your case the variable on the left is an int, so the only thing that can be stored there is an integer. You can use a cast anywhere in an expression on the right-hand side of the equals sign, but not the left: CPP / C++ / C Code:
A few runs: Code:
In the case of a double variable being assigned to an int, it is not converted to the nearest integer value, it is truncated. (The fractional just goes away). Didn't the book mention that it is truncated, not rounded? Now, it happens that, with C and C++, if a conversion is required in order to make an assignment of one numerical data type to another numerical data type, it is done automatically by the assignment statement. Try the example again without the cast. That is, change the assignment statement to CPP / C++ / C Code:
What happens? Regards, Dave |
|
#3
|
|||
|
|||
Re: Converting numbers using explicit type conversion (casting)Quote:
There is an implicit type conversion if I do what you told me to do. But, what would I have to do in order to round the number to the nearest integer, instead of just truncate the decimal part? Thanks in advance! A.J. Foucault |
|
#4
|
|||
|
|||
Re: Converting numbers using explicit type conversion (casting)Quote:
How do you do it "by hand?" If you round 3.14 to the nearest integer what do you get? If you round 3.64 to the nearest integer what do you get? If you round -3.14 to the nearest integer what do you get? If you round -3.64 to the nearest integer what do you get? How can you make a general rule (that can be implemented in a program)? Consider: If x is greater than or equal to zero, what happens if you add 0.5 to the decimal number and truncate so that the fractional part goes away? Apply this "rule" to the positive number examples that I showed. Try some others "by hand." Then write the part of the program that deals with positive numbers. Then... What about the case when x is less than zero? What would you do? Regards, Dave Footnote: The so-called C99 standard C language library math header <math.h> has a function called round() that rounds a double to the nearest integer value that is representable by a double precision variable. Some C++ compilers have this function (In C++ you would use <cmath>) Some compilers do not have this, but I think that even if your compiler library has the round() function, thinking about what goes on "under the hood" is a valuable learning experience. |
|
#5
|
|||
|
|||
Re: Converting numbers using explicit type conversion (casting)If you round 3.14 to the nearest integer what do you get?
3 If you round 3.64 to the nearest integer what do you get? 4 If you round -3.14 to the nearest integer what do you get? -3 If you round -3.64 to the nearest integer what do you get? -4 Am I right? When x is LESS THAN zero, and the decimal part is .51 or more, I would round off to the nearest integer using as a reference the number line. |
|
#6
|
|||
|
|||
Re: Converting numbers using explicit type conversion (casting)Quote:
Your rounded answers are, of course, correct. Now the trick is to use your knowledge of what happens in a program if you truncate. Look back at the examples from the code in my first post. ------------------------------------- If you truncate 3.14 you get 3 If you truncate 3.64 you get 3 If you have a number that is between 3 and 4 and it is closer to 4 than it is to 3, how can you get the answer to be 4? Add 0.5 before truncating. Right? So here's how I propose to make a program to perform rounding of non-negative numbers: Code:
------------------------------------- Now: If you truncate -3.14 you get -3 If you truncate -3.64 you get -3 If you have a number that is between -3 and -4 and it is closer to -4 than it is to -3, how can you get the answer to be -4? Subtract 0.5 before truncating. Right? So here's how I propose to make a program to perform rounding of negative numbers: Code:
------------------------------------- One additional thing to consider: What if a number is halfway between two integers? How would you round it? What happens if you are given 1.5 as an input? What happens if it is 2.5? What if it is -1.5? What if it is -2.5? The "naive" convention (and one that seems to be used in most programs) is that numbers with a fractional part exactly equal to 0.5 get rounded to the next integer with larger magnitude (so 1.5 is rounded to 2, 2.5 is rounded to 3, -1.5 is rounded to -2 and -2.5 is rounded to -3). Does the scheme that I proposed work that way? Is that OK? In other words, what, exactly, was the program specification? Regards, Dave "No one was born knowing this stuff. Right?" ---davekw7x |
Recent GIDBlog
Toyota - 2009 May Promotion by Nihal
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Problem executing nam-1.13 | RodolfoAlvizu | Computer Software Forum - Linux | 20 | 28-Feb-2009 16:23 |
| Hard drive/CPU Diagnoses Issues | binarybug | Computer Hardware Forum | 1 | 22-Jan-2007 20:23 |
| Converting letters that I type to a * help | MiserableMad | .NET Forum | 0 | 07-Sep-2006 17:48 |
| C - converting words into numbers | selent | C Programming Language | 8 | 12-Dec-2005 13:37 |
| Type casting problems | Dream86 | C++ Forum | 2 | 25-Jul-2005 15:49 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The