Values getting stuck in memory???

I have a school project where I have to create a class with 2 attributes that default to 1. To verify that they are at 1, I have a cout before any user inputs or value manipulation. It works for the most part. What happens is that if I change the defaults to 17, the cout will retain the old values until I have an unsuccessful build (remove a semicolon from the end of the line). When I have a successful build (put that semicolon back), the cout displays the changed values. Change the values back to 1 and the same thing happens again until I have an unsuccessful build then a successful build. Are the old values getting stuck in memory somewhere?

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
124
125
126
127
128
129
130
131
132
// Rectangle.h
// Chapter 9_Problem 9.11_Page 412

#include <iostream>
using namespace std;
#ifndef RECTANGLE_H
#define RECTANGLE_H

class Rectangle // Rectangle class
{
public:
	Rectangle(double = 17, double = 17); // default constructor setting values to 1
	void startProgram();
	void setLength (double l); // setter
	void setWidth (double w); // setter
	double getLength(); // getter
	double getWidth(); // getter
	double perimeter();
	double area();
	double width;
	double length;
	double w;
	double l;



	
private:

	
};

#endif



// Rectangle.cpp
// Chapter 9_Problem 9.11_Page 412

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

Rectangle::Rectangle(double l, double w)
{
	setLength(l);
	setWidth(w);
}

void Rectangle::setLength(double l)
{
	if ((l < 0) || (l > 20))
	{
		length = 1;
	}
	else
	{
		length = l;
	}

}

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

void Rectangle::setWidth(double w)
{
	if ((w < 0) || (w > 20))
	{
		width = 1;
	}
	else
	{
		width = w;
	}

}


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

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

double Rectangle::perimeter()
{
	return 2*(length + width);
}


void Rectangle::startProgram()
{
	

	cout << "Calculating the area and perimeter of a rectangle based on user inputs.\n" << endl;
	cout << "Initial value of length is: " << length << endl;
	cout << "initial value of width is: " << width << endl;
	cout << "Input the length of the rectangle (or -1 to quit): " << endl;
	cin >> length;

	while (length != -1)
	{
		cout << "Input the width of the rectangle: " << endl;
		cin >> width;

		cout << "Length" << setw(10) << "Width" << setw(15) << "Area" << setw(20) << "Perimeter" << "\n";
		cout << fixed << setprecision(1) << getLength() << setw(11) << getWidth() << setw(17) << area() << setw(15) << perimeter() << endl;



		cout << "\n\nInput the length of the rectangle (or -1 to quit): " << endl;
		cin >> length;

	}

	cout << "\nThanks for playing.\n" << endl;


}




Last edited on
From the sound of it, your compiler isn't recompiling Rectangle.cpp when Rectangle.h is changed. When you say you remove a semicolon from a line, is that from the header file, or the source file?

From the header file. I remove the semicolon in line 12.
OK, that's weird. What compiler are you using?
MS Visual Studio 2010 Professional. I just restarted VS and it still does it.
For completeness, here is my main .cpp.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// Rectangle_main.cpp
// Chapter 9_Problem 9.11_Page 412


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

int main()
{
    Rectangle Rectangle;
    Rectangle.startProgram();

    return 0;
}



MS Visual Studio 2010 Professional. I just restarted VS and it still does it.

I'm not the world's biggest MSVC expert, but I'm guessing it's something like the minimal compilation feature or something like that, which tries to work out whether it can get away without compiling everything that depends on the header file. I would try turning that option off in the project settings, along with the precompiled header usage, and seeing if it still does it.
I'll look for that in the project settings.

I changed the default values then did a "clean project", whatever that is. Then a build and it works fine. So whatever "clean" does fixes it.
A clean build is when you force the compiler to delete all of the results of the previous build, and rebuild everything from scratch. So, by definition, it will always recompile your file and pick up your changes.

But you don't want to have to do that every time. Hopefully, changing those project settings will fix things.
Changing that setting worked.

I found the setting for "Enable Minimal Rebuild". Set that to 'no' and it now works.

Thanks for your help.
You're welcome. Glad it worked out :)
Topic archived. No new replies allowed.