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:
Code:
#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:
Code:
#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:
Code:
#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";
}
};