The program is supposed to ask the user for the length. then check if the length inputted is greater than zero, and if it is then the user's length will be placed in the class attribute "length". If the length is not greater than zero, an error message will be displayed prompting the user to try again until a valid value is entered. The program is supposed to do the SAME THING for the width. Then the program will return the area (lengthXwidth). Now....the program does this perfectly for the length, but when i input a width the program will not give me an error message if it is below zero. The program will simply accept the invalid width. My program also displays the length and width used to calculate the are. the width displayed in this case is not even what i input , but some weird number.
int main()
{
Rectangle box; // instantiation of box object in Rectangle class
double boxlength,
boxwidth; //variables to store length and width in main.
cout << "Please enter the length of your box" << endl; //ask user for length
cin >> boxlength; //retrieve length from user
while(!box.setLength(boxlength)) // if length is below zero, notify user that it is invalid.
{
cout << "I am sorry, but you have entered an invalid length, please"
<< " try again" << endl;
cin >> boxlength; //ask for length again until a valid value is given
}
cout <<" Now please enter the width of your box" << endl; //ask user for width
cin >> boxwidth; //retrieve width from user
while(!box.setWidth(boxwidth)) //if width is below zero, notify user that it is invalid
{
cout << "I am sorry, but you have entered and invalid width, please"
<< " try again" << endl;
cin >> boxwidth; // ask for width until a valid value is given
}
cout << "For your box having dimensions: " << endl; // Restate the dimensions entered by the user.
cout << "Length-> " << box.getLength() << endl;
cout << "Width-> " << box.getWidth() << endl;
cout << "The area is: " << box.getArea(); //calculate the area of the box.
cout << "\nThank you." << endl;
If that is the case i would think that the length portion wouldn't work as well because the code is the same...but i will look into that. thanks koothkeeper