GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 31-May-2009, 18:09
ajfoucault ajfoucault is offline
New Member
 
Join Date: May 2009
Posts: 3
ajfoucault is on a distinguished road
Exclamation

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:
error C2106: '=' : left operand must be l-value c:\users

Anyways, here's my code. Thanks in advance to all of you for your help!

CPP / C++ / C Code:
#include <iostream>

using namespace std;

int main()
{
	double num1, num2, num3, num4, num5;	//Declaration of the variables that will hold the 5 decimal numbers
	int int1;

	cout << "Please enter five (5) decimal numbers: "; //Prompts the user to input 5 decimal numbers
	cin >> num1 >> num2 >> num3 >> num4 >> num5;

	// Prints the five decimal numbers
	cout << "The five (5) decimal numbers that you entered are: " 
	<< num1 << " " << num2 << " " << num3 << " " << num4 << " " << num5;

	// Converts each decimal number to the nearest integer;
    static_cast<int>(num1) = int1;
	
	cout << int1;
	system("pause");
	return 0;
}
  #2  
Old 31-May-2009, 19:00
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Converting numbers using explicit type conversion (casting)


Quote:
Originally Posted by ajfoucault
...
error C2106: '=' : left operand must be l-value c:\users

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:
#include <iostream>

using namespace std;

int main()
{
    double x;
    int    i;

    cout << "Enter a decimal number: ";
    cin >> x;
    cout << "You entered " << x << endl;

    i = static_cast<int>(x);
    cout << "The truncated integer value is " << i << endl;

    return 0;
}

A few runs:

Code:
Enter a decimal number: 3.14 You entered 3.14 The truncated integer value is 3 Enter a decimal number: 3.99 You entered 3.99 The truncated integer value is 3 Enter a decimal number: -3.14 You entered -3.14 The truncated integer value is -3 Enter a decimal number: -3.99 You entered -3.99 The truncated integer value is -3

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:
    i = x;

What happens?

Regards,

Dave
  #3  
Old 01-Jun-2009, 19:17
ajfoucault ajfoucault is offline
New Member
 
Join Date: May 2009
Posts: 3
ajfoucault is on a distinguished road
Smile

Re: Converting numbers using explicit type conversion (casting)


Quote:
Try the example again without the cast. That is, change the assignment statement to
CPP / C++ / C Code:

i = x;


What happens?

Regards,

Dave

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  
Old 01-Jun-2009, 22:48
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Converting numbers using explicit type conversion (casting)


Quote:
Originally Posted by ajfoucault
...what would I have to do in order to round the number to the nearest integer,...

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  
Old 02-Jun-2009, 17:32
ajfoucault ajfoucault is offline
New Member
 
Join Date: May 2009
Posts: 3
ajfoucault is on a distinguished road

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  
Old 02-Jun-2009, 18:39
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Converting numbers using explicit type conversion (casting)


Quote:
Originally Posted by ajfoucault
...Am I right?

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:
If the number is greater than (or equal to) zero, then add 0.5 and truncate.
Try it with a bunch of different positive numbers. Does it always work?

-------------------------------------

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:
If the number is less than zero, then subtract 0.5 and truncate.
Try it with a bunch of negative numbers. Does it always work?

-------------------------------------



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 GIDBlogToyota - 2009 May Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 16:27.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.