Thread: My float variable gets erased when defined in constructor.

  1. #1
    Registered User
    Join Date
    Mar 2018
    Posts
    2

    Question 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:

    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";
            }
        };

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Code:
        // constructor
        BoxClass::BoxClass(int x, int y, int amount) {
            /*float height_Count = 1;
            bool error = false;
            */
    Do you realize that the above code would create two local instances of those variables that would go out of scope when the function ends?

    If you want to initialize the variable in the constructor body you would need to assign a value to the variable not create a new instance of the variable.

    By the way if you're compiling to one of the current modern C++ standards initializing the variable in the header is an acceptable way of initializing the variable. You could also use an initialization list as another alternative. But it is considered a poor practice to initialize the variables inside the constructor body, especially if the variable is anything other than a POD type.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 02-27-2010, 03:20 AM
  2. User defined Variable name
    By mat429 in forum C Programming
    Replies: 4
    Last Post: 08-28-2008, 08:07 AM
  3. Variable being used without being defined error
    By Moony in forum C Programming
    Replies: 2
    Last Post: 06-25-2006, 10:09 AM
  4. outputting a defined variable
    By rotis23 in forum C Programming
    Replies: 2
    Last Post: 11-12-2002, 05:00 AM
  5. Replies: 2
    Last Post: 02-28-2002, 03:27 PM

Tags for this Thread