My float variable gets erased when defined in constructor.

What the program does: My programs prints out a/multiple rectangle(s)/squar(s) defined by the user's input of width, height, and ammount. The program can also give the area of the box in terms of empty spaces in the rectangle.

I'm back! Whenever I define my "height_Count" variable in the header file with the other declared variables, the "Rectangle_Area()" function works and the "height_Count" variable works correctly, but whenever I define the variable in my constructor it seems to make the float variable -1.005460... I dont know why it doesnt just keep its value.

You can find the variable declaration/definition in the header file. The constructor is in the BoxClass.cpp file towards the bottom. I put the program code that worked. So the "height_Count" is defined in the header file instead of the constructor. I have the "height_Count" variable as a comment in the constructer so it doesnt define there. Again im asking for someone to move the "height_Count" variable to the constructor, set it equal to 1, and have that variable not flip out when running the program.

ConsolApplication1.cpp:

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
#include "stdafx.h"
#include "BoxClass.h"
#include <iostream>
using namespace std;


int main() {
	//variable declaration/definition
	int width_Var;
	int height_Var;
	int number_of_Boxes;

	//object declaration/Body
	cout << "Enter width of rectangle/box\nWidth = ";
	cin >> width_Var;
	cout << "Enter height of rectangle/box\nHeight = ";
	cin >> height_Var;
	cout << "How many rectangles/boxes do you want?\n";
	cin >> number_of_Boxes;

	BoxClass box1(width_Var, height_Var, number_of_Boxes);
	
	cout <<"Rectangle Area = "<<box1.Rectangle_Area() << endl;

//exit
	cout << "\n\n\n\n\n";
	system("pause");
    return 0;
}



BoxClass.h:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef BOXCLASS_H
#define BOXCLASS_H

class BoxClass {
	//prv variables
	unsigned short int width;
	int height, i;
	float space_Value;
	float height_Count=1;
	bool error=false;
	//prv functions
	void Print_Rectangle(int x, int y);

public:
	//function shows area of individual spaces
	float Rectangle_Area();

	// constructor
	BoxClass(int x, int y, int amount);
};

#endif 


BoxClass.cpp

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
#include "stdafx.h"
#include "BoxClass.h"
#include <iostream>
using namespace std;


	void BoxClass::Print_Rectangle(int x, int y) {
		//calc
		space_Value = (3 * x) - 4;

		//draw top of box
		for (width = 1; width < x; width += 1) {
			cout << "...";
		}
		cout << "\n";

		//draw sides
		for (height = 1; height < y; height += 1) {
			cout << ":";
			height_Count = height_Count + 1;

			for (width = 1; width < space_Value; width += 1) {
				cout << " ";
			}

			cout << ":\n";
		}

		//draw bottom
		cout << ":";

		for (width = 1; width < space_Value; width += 1) {
			cout << ".";
		}
		cout << ":\n";
	}


	//function shows area of individual spaces
	float BoxClass::Rectangle_Area() {
		

		if (error == true) { cout << "Failed\n"; }
		else if (error == false) { cout << "Worked\n"; }

		if (error == false) {
			return (height_Count - .5)*(space_Value - 1);
		}
		else {
			return 0;
		}
	}

	// constructor
	BoxClass::BoxClass(int x, int y, int amount) {
		/*float height_Count = 1;
		bool error = false;
		*/
		if (x <= 41) {
			for (i = 1; i <= amount; i += 1) {
				Print_Rectangle(x, y);
			}
		}
		else {
			error = true;
			cout << "Error - width must be below 42!\n";
		}
	};
Your variable height_Count is declared (given the type float) when the class is declared in BoxClass.h.

If you write float height_Count in your constructor then you will be creating another LOCAL variable which will hide the outside one of the same name and which will disappear at the end of the constructor.

Simply remove the word "float" for this variable in the constructor.
Last edited on
@lastchance Thank you so much!
Topic archived. No new replies allowed.