division returns same number regardless of numbers used

Hello, I'm having a bit of a problem with a school assignment. The getWeight function seems to return 54 no matter what numbers I use. I've wasted at least 45 minutes trying to figure it out. Here is part of the code. Thanks.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include "stdafx.h"
#include <iostream>
using namespace std;

#include "Animal.h"
#include "Lion.h"
#include "Dog.h"

void setHeightWeight( Animal *) ; 

int main()
{
   Dog dog1( 60, 120, "Fido" );
   Lion lion1( 45, 300 );

   Animal *aPtr;
   aPtr = &lion1;
   setHeightWeight( aPtr ); //
   aPtr = &dog1;
   setHeightWeight( aPtr ); //
} // end main

// function setHeightWeight definition
void setHeightWeight( Animal *a  )
{
   int height;
   int weight;

   a->print();
   cout << "Enter a new height (using standard units): ";
   cin >> height;
   a->setHeight( height );

   cout << "Enter a new weight (using standard units): ";
   cin >> weight;
   a->setWeight( weight );

   height = a->getHeight();
   weight = a->getWeight();

   cout << "Here are the new height and weight values:\n"
        << height << endl
        << weight << endl << endl;
} // end function setHeightWeight

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "stdafx.h"
#include <iostream>
using namespace std;

#include "Dog.h"


Dog::Dog( int h, int w, string n )
   : Animal( h, w )
{
   setName( n );
   metricHeight = h * 2.54;//1 in = 2.54 cm
   metricWeight = w / 2.2;

} 


string Dog::getName() const 
{
   return name;

} // end function getName

// function setName definition
void Dog::setName( string n ) 
{
   name = n;

} 

// function print definition
void Dog::print() const
{
   cout << "This animal is a dog, its name is: "
        << name << endl;
   Animal::print();

} // end function print

// return height
int Dog::getHeight() const 
{
   if ( useMetric( "height" ) )
      return metricHeight;

   else
      return Animal::getHeight();

} // end function print

// return weight
int Dog::getWeight() const 
{
   if ( useMetric( "weight" ) )
      return metricWeight;

   else
      return Animal::getWeight();

} // end function getWeight

// function useMetric definition
bool Dog::useMetric( string type ) const
{
   int choice = 0;

   cout << "Which units would you like to see the "
        << type << " in? (Enter 1 or 2)\n"
        << "\t1. metric\n"
        << "\t2. standard\n";

   cin >> choice;

   if ( choice == 1 )
      return true;

   else
      return false;

} // end function useMetric

int Dog::getWeight() constchange the int to a float and watch the magic. :)
Last edited on
hmmm.... unfortunately with type float I still get 54. By the way it only happens if I get the metric weight from a dog object. Also if I put in a weight of 116 I should get 52 when getting it in metric using integers. 116/2.2 = 52. I think it could also be some bad pointer logic in the main. I'm not particularly sure. Any number I use gives a result of 54
is metricWeight an integer?
yes. Everything has to be an integer in the program. It's a debugging assignment as well. For the dog when you specify a weight of 116 then ask for the metric it should give 52.
getWeight and getHeight are virtual functions as well. They override the same named functions in the base class. In the base class theyre all just simple get and set
Last edited on
so you always get 54 as the output? no matter what you initialize dog as? if that is not the case then your program is working as it should.
I haven't set it to anything else in the object instantiation, but in that function called in main a setWeight is used. Whatever the weight is set at the getWeight always returns 54 if I get the metric instead of standard
Dog dog1( 60, 120, "Fido" );

Dog::Dog( int h, int w, string n )
: Animal( h, w )
{
setName( n );
metricHeight = h * 2.54;//1 in = 2.54 cm
metricWeight = w / 2.2;

}
that is a constructor. that is why you get 54 because 120/2.2 is ~54
ah thanks! so that means my setWeight is somehow not working in either the base class or the derived class or maybe even the pointers screw it up somehow. I'll take a look.
So I just have to override the setWeight function because when the setWeight is called it only uses the one in the base class and in the base class there is no metric change so the original in the instantiation always stays. Thanks.
Topic archived. No new replies allowed.