Re: Value convertion problem

From: Robert W Hand (rwhand_at_NOSPAMoperamail.com)
Date: 04/12/04

  • Next message: soop: "Re: Don't understand (ofstream problem)"
    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
    

  • Next message: soop: "Re: Don't understand (ofstream problem)"

    Relevant Pages

    • Liar Liar
      ... random aa's or a maximally distant sequence. ... is not the same as the likely gap distance and my calculations reflect ... Your notion that the minimum likely distance stays at the ...
      (talk.origins)
    • Re: Cosmic acceleration rediscovered
      ... <snip stuff resulting from my typo> ... >> large scale variation with distance. ... not cosmological expansion) are not a factor in tired light ... > is redshift versus magnitude or some other indirect measure of distance." ...
      (sci.astro)
    • Econuts avoidance of the truth
      ... which form of recreation causes more erosion.. ... Your "lit review" claims the Wilson and Seney quote you gave as a reason to question the results...but if you include the full quote, ... It's where you conveniently snip out the fact that the evening mean movement rate of elk for mountain bike events was the same as hiking events. ... And one more thing....have you figured out the difference between speed and distance yet? ...
      (alt.mountain-bike)
    • Re: Have you ever wondered.....
      ... > measuring is consistant with the definition of distance. ... So, when you 'observe' the distance between two spatial points, you ...
      (sci.physics.relativity)
    • Re: God as collective consciousness
      ... property of the material universe. ... the energy is released at a steady rate. ... Distance relies on rulers. ...
      (talk.origins)

    Loading