Help with Command Line Program Issue.

Hello, everyone I have a question as to why when my negatives inputs do nut turn into zeros. it gives out a lot of random numbers.


#include <iostream>
using namespace std;
class Rectangle
{
private:
double myLength;
double myWidth;
public:
//Accessor And Mutator
void SetLength(double newLength);
double GetLength() const;
void SetWidth(double newWidth);
double GetWidth() const;
double GetRectangleArea() const;
double GetRectangleParameter() const;
};
int main()
{
Rectangle theRectangle;
double userLength, userWidth;
cout << "Please Enter The Rectangle's Length: ";
cin >> userLength;
cout << "Please Enter The Rectangle's Width: ";
cin >> userWidth;
theRectangle.SetLength(userLength);
theRectangle.SetWidth(userWidth);
cout << theRectangle.GetLength() << " Was Input For The Length." << endl;
cout << theRectangle.GetWidth() << " Was Input For The Width." << endl;
cout << "The Area Of The Rectangle Is " << theRectangle.GetRectangleArea() << endl;
cout << "The Parameter Of The Rectangle Is " << theRectangle.GetRectangleParameter() << endl;
return 0;
}
void Rectangle::SetLength(double newLength)
{
if (myLength < 0.0)
{
newLength = 0.0;
}
else
myLength = newLength;
}
double Rectangle::GetLength() const
{
return myLength;
}
void Rectangle::SetWidth(double newWidth)
{
if (myWidth < 0.0)
{
myWidth = 0;
}
else
myWidth = newWidth;
}
double Rectangle::GetWidth() const
{
return myWidth;
}
double Rectangle::GetRectangleArea() const
{
return myLength*myWidth;
}
double Rectangle::GetRectangleParameter() const
{
double lengthParameter;
double widthParameter;
lengthParameter = myLength * 2;
widthParameter = myWidth * 2;
return lengthParameter + widthParameter;
}

For example:
If I input the number -5 and 6 i get the output:

Please Enter The Rectangle's Length: -5
Please Enter The Rectangle's Width: 6
-9.25596e+61 Was Input For The Length.
0 Was Input For The Width.
The Area Of The Rectangle Is -0.
The Parameter Of The Rectangle Is -1.85119e+62.
Press Any Key To Continue.
You are mixing up the two variables myLength and newLength.

Here,
1
2
3
4
if (myLength < 0.0)
{
    newLength = 0.0;
}

You should be testing whether the user input newLength is negative, and assigning zero to member variable myLength. Your code has them swapped.

By the way, where you refer to "The Parameter Of The Rectangle" the term should be Perimeter. (easy to get confused).
http://www.dictionary.com/browse/perimeter
such a simple mistake T.T. Thanks for the information aha. :D also aha my bad I got parameter mixed up with perimeter, haven't used those words in a while. :P
Last edited on
Topic archived. No new replies allowed.