No errors, but the program doesnt do what i want. class header included.

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.

Class specification File:

#ifndef RECTANGLE_H
#define RECTANGLE_H

class Rectangle
{
private:
double length,
width;
public:
bool setLength(double);
bool setWidth(double);
double getLength();
double getWidth();
double getArea();
};

#endif // RECTANGLE_H_INCLUDED



Class implementation File:

#include "Rectangle.h"

bool Rectangle::setLength(double l)
{
bool validData=true;
if(l>0)
length=l;
else
validData=false;
return validData;
}





bool Rectangle::setWidth(double w)
{
bool validData=true;
if(w>0)
width=w;
else
bool validData=false;
return validData;
}






double Rectangle::getLength()
{
return length;
}




double Rectangle::getWidth()
{
return width;
}





double Rectangle::getArea()
{
return width*length;
}






Main:

#include <iostream>
#include <cstdlib>
#include <cmath>
#include "Rectangle.h"

using namespace std;

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;

}
Last edited on
You are probably having problems with cin. Your code looks fine to me. Re-read the page on this site about cin.
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
Hint: There might be stray characters in the input.
haha...that literally just came to me as i was opening your reply..will give it a try.
Every DogMan has his day ... ;)
i used cin.ignore()...but no luck.
lol...and this is not one of mine
1
2
3
4
5
6
7
8
9
bool Rectangle::setWidth(double w)
{
bool validData=true;
if(w>0)
width=w;
else
bool validData=false;
return validData;
}


1
2
3
bool validData=true;
//...
bool validData=false;
Last edited on
im sorry but i dont understand your comment
You declared validData in your function twice. Remove "bool" from the line right above the return.
wow...something so trivial..i need some sleep. Thank you so much fg109.
Topic archived. No new replies allowed.