Re: Value convertion problem
From: Robert W Hand (rwhand_at_NOSPAMoperamail.com)
Date: 04/12/04
- Previous message: Leor Zolman: "Re: do anyboby know how to convert binary to decimal and hexadecimal to decimal"
- In reply to: Chiller: "Value convertion problem"
- Next in thread: Robert W Hand: "Re: Value convertion problem"
- Reply: Robert W Hand: "Re: Value convertion problem"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 12 Apr 2004 08:24:20 -0400
On Mon, 12 Apr 2004 07:59:43 GMT, "Chiller" <...@...> wrote:
>Ok, I've implemented a few changes to the code and the bool functions now
>seem to be functioning correctly; however, I think I'm doing the convertions
>incorrectly because the values printed out from my TEST_DISTANCE driver
>haven't converted correctly. Also, when outputing the Distance objects,
>numbers are displayed but the value isn't displayed correctly. ie I'm
>getting erroneous characters rather than a simple m etc.
I had to change a few minor things to get it to compile with Comeau.
But I believe that the problem is the type for km, mm etc. Please see
below.
>.cpp file
>**************************
>#include "Distance.h"
>
>#include <iostream>
>
>using namespace std;
>
>/*-------------------------------------------------------*\
>
>| implementation of member functions |
>
>\*-------------------------------------------------------*/
>
>// constructors
>
>Distance :: Distance (int n, char m) : nu(n), me(m) {}
>
>Distance :: Distance (void) : nu(0), me('m') {}
>
>enum Measure {mm=1, cm=10, m=1000, km=1000000};
mm, cm, m, and km will have type int on my system. Measure must have
type to hold these values. It will not fit into a char.
>// Convert mm value into metres
>
>int Distance::conv()
>
>{
>
>return (nu = nu * 1000, me ='m');
This syntax is unusual. First I would do
nu*=1000;
me = 'm';
Then I would not return anything and make the conv return void.
But it seems to me that you need to check to see what unit your
measurement is in before multiplying. Let's say that me is already in
meters. Then you would not do any multiplying. And there is the
problem of slipping between enumerations for me and characters. You
need to pick one or the other.
<snip>
>
>// Overload of the "==" operator
>
>bool Distance::operator == ( Distance const & rhs )
>
>{
>
>conv();
>
>return ( nu == rhs.nu);
>
>}
I don't get this one. You are invoking conv() on the left hand
operand, but not the right hand one. How do we know that they have
the same units? All the relational operators appear to be written in
the same manner.
<snip>
>//Overload of the + (addition) operator
>
>Distance Distance::operator+ (Distance right_operand)
>
>{
>
>conv();
>
>return Distance (nu + right_operand.nu, me);
Once again, I am not sure if the two operands are in the same units.
<snip>
>| test driver for the Distance class |
>
>\*-------------------------------------------------------*/
>
>#ifdef TEST_DISTANCE // .... Distance class .... test driver
>
>int main (void)
>
>{
>
>// create test input
>
>Distance a = Distance (6, cm);
cm is 10. So you will have some funny symbol in me.
>
>Distance b (4, km);
km is one million, so there will be truncation to get this to run.
Then there is the implementation-defined behavior of truncating into a
potentially signed integer type.
<snip>
>#endif
>
>
>
>.h file
>***************************
>
>#ifndef DISTANCE_H
>
>#define DISTANCE_H
>
>#include <iostream>
>
>using namespace std;
>
>class Distance
>
>{
>
>public :
>
>Distance (int, char) ; // constructor - takes int and char values
>
>Distance (void) ; // default - zero
>
>//access member functions
>
>int number (void);
>
>char measure (void);
>
>//overloads
>
>Distance & Distance::operator= (Distance const & right_operand);
You've done this several places. The qualifier Distance is not
needed, and it should not compile.
The major problem appears to be in the unit of measurement. I'd leave
it an enum. You do not need such large differences in the
enumeration, do you? I have compiled and run it once. I get bizarre
results. Try to make sure that you are using the same units in your
comparisons. In conv(), you do not seem to check the unit type before
converting. HTH.
-- Best wishes, Bob
- Previous message: Leor Zolman: "Re: do anyboby know how to convert binary to decimal and hexadecimal to decimal"
- In reply to: Chiller: "Value convertion problem"
- Next in thread: Robert W Hand: "Re: Value convertion problem"
- Reply: Robert W Hand: "Re: Value convertion problem"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|