Range check is not occuring?

In my program I have a range check setup in the class I call from main, but when I run it and put in a value > or < than the min/max it just calculates anyway. Where have I made a mistake?

Heres the Main
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

#include "box_class.h"
#include <iostream>
using namespace std;

     
int main()
{
	double length;
    double width;
    double height;
    double volume;
    Box box;
    
    
	cout << "Please Enter a Length for your box: " ;
	cin >> length;
	box.setLength(length);
	cout << "Please Enter a Width for your box: " ;
	cin >> width;
	box.setWidth(width);
	cout << "Please Enter a Height for your box: " ;
	cin >> height;
	box.setHeight(height);
	

	box.toString();
	volume = box.getVolume();
	cout << "\nThe Volume of your box is: " << volume << " \n_____________________________________________\n";
	//box.getParameters();

	return 0;
}


Heres the Class
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <iostream>
#include <stdexcept>
#include <sstream>
#include <iomanip>
#include <string>

using namespace std;

class Box
{
    public:
		double length;//length of the box
		double height;//height of the box
		double width;//with of the box
		
		Box(): length(1), height(1), width(1){}
		
		//Parameterized Constructor
		Box(double length, double width, double height);
		
		
		double getVolume(void);
 		
		//Mutators
		void setLength(double leng);
		void setWidth(double wid);
		void setHeight(double hei);
		//Acessors
		void toString();
		double getLength(double length); 
		double getWidth(double width);
		double getHeight(double height);
};//end class	

//member function definitions
	void Box::setLength(double leng)
	{
		const double MIN_LENGTH = 0;//constants for min/max for range check and out_of_range exception
		const double MAX_LENGTH = 99;
		if (length > MAX_LENGTH || length <= MIN_LENGTH)
		{
			stringstream strOut;//declare string stream
			
			strOut << "Length is out of range. Length must be between" << MIN_LENGTH << " and " << MAX_LENGTH << ".";//error msg
			throw out_of_range(strOut.str());
		}
		else if (length < 0.01){
		
			length = 0.01;
			length = leng;
		}
		else
		{
			length = leng;// if length is within range, store it 
		}
	}
	double Box::getLength(double length)
	{
		return length;
	}
	void Box::setWidth(double wid)
	{
		const double MIN_WIDTH = 0;//constants for min/max for range check and out_of_range exception
		const double MAX_WIDTH = 99;
		if (width > MAX_WIDTH || width <= MIN_WIDTH)
		{
			stringstream strOut;//declare string stream
			
			strOut << "Width is out of range. Width must be between" << MIN_WIDTH << " and " << MAX_WIDTH << ".";//error msg
			throw out_of_range(strOut.str());
		}
			else if (width < 0.01)
		{
			width = 0.1;
			width = wid;
		}
		else
		{
			width = wid;// width is in range, store it
		}
	}	

	double Box::getWidth(double width)
	{
		return width;
	}
	void Box::setHeight(double hei)
	{
		const double MIN_HEIGHT = 0;//constants for min/max for range check and out_of_range exception
		const double MAX_HEIGHT = 99;
		if (height > MAX_HEIGHT || height <= MIN_HEIGHT)
		{
			stringstream strOut;//declare string stream
			
			strOut << "Height is out of range. Height must be between" << MIN_HEIGHT << " and " << MAX_HEIGHT << ".";//error msg
			throw out_of_range(strOut.str());
		}
		else if (height < 0.01) // check if height is less than 0.1 if it is, store 0.1
		{
			hei = 0.01;
			height = 0.1;
		}
		else
		{
			height = hei;// height is in range, store it
		}
	}	
	double Box::getHeight(double height)
	{
		return height;
	}
		double Box::getVolume(void)//get volume will call and output the volume when called
	{
		return length * width * height;
	}
	void Box::toString()
	{
		
		cout << "\n\nDimensions\n_____________________________________________\nLength: " << getLength(length) << endl 
		<< "Width: " << getWidth(width) << endl 
		<< "Height: " << getHeight(height) << endl;
	}
You are validating the member variables instead of the parameters.
so change length.. to leng/wid/hei?
Yes, since you want to validate the parameters.
Topic archived. No new replies allowed.