Object values being reset

I'm working with two classes and I find that everytime I call an object, their values are being reset to their initial values.

For my code below, if I call object1, modify its values, return to main, then call the object again, I can see that their values have reset and I don't know what's causing this.

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
#include <iostream>
#include <iomanip>
using namespace std;
#include "Circles.h"
void menu();
void operations(Circles sphere);


int main()
{
	Circles sphere1;
	Circles sphere2;
	
	while (true) {
		cout << "Which circle would you like to interact with, or find the distance between the 2 circles? [1|2|3] \n ";
		int op;
		cin >> op;
		if (op == 1) {
			menu();
			operations(sphere1);
		}
		else if (op == 2) {
			menu();
			operations(sphere2);
		}
		else if (op == 3) {
			double distance = sphere1 - sphere2;
			cout << "The distance between the two circles is: " << fixed << setprecision(3) << distance << endl;
			break;
		}
		else {
			cout << "Invalid entry\n";
			break;
		}
	}
	system("pause");
	return 0;
}

void menu() {
	cout << "Which Operation would you like to perform?\n" << " 1: Set Center\n " << "2: Set Radius \n " << "3: Get Center \n " << "4: Get Radius \n " << "5: Get Circumference \n " << "6: Get Area \n " 
		<< "7: Return to main \n ";
}

void operations(Circles sphere) {
	int x1, y1;
	int radius;
	double rad = sphere.getRadius();
	double circumference = sphere.calcCircumference();
	double area = sphere.calcArea();
	int op;
	while (true) {
		cin >> op;
		switch (op) {
		case 1:
			cout << "enter x coordinate: ";
			cin >> x1;
			cout << "enter y coordinate: ";
			cin >> y1;
			sphere.setCenter(x1, y1);
			menu();
			break;
		case 2:
			cout << "Enter Radius: ";
			cin >> radius;
			sphere.setRadius(radius);
			menu();
			break;
		case 3:
			sphere.printStats();
			menu();
			break;
		case 4:
			cout << "Radius: " << rad << endl;
			menu();
			break;
		case 5:
			cout << "Circumference " << circumference << endl;
			menu();
			break;
		case 6:
			cout << "Area: " << area << endl;;
			menu();
			break;
		case 7:
			return;
		default:
			cout << "Invalid entry\n";
			break;
		}
	}
}



and if it helps here are my header and implementation files for my 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
#include <iostream>

using namespace std;

#ifndef CIRCLES_H
#define CIRCLES_H
class Circles{
public:
	Circles(double r);    // Constructor
	Circles();             // Default constructor
	Circles(double r, int c1, int c2);

	void setCenter(int x, int y);
	double calcArea();
	double calcCircumference();
	void printStats();     // This outputs the radius and center of the circle.
	void setRadius(double r);
	double getRadius() const;

	double Circles::operator-(const Circles &r) const;


private:
	double  radius;
	int     center_x;
	int     center_y;
};
#endif 


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
#include "stdafx.h"
#include "Circles.h"

Circles::Circles()
{
	radius = 1;
	center_x = 0;
	center_y = 0;
}

Circles::Circles(double r)
{
	radius = r;
}
Circles::Circles(double r, int c1, int c2) {
	radius = r;
	center_x = c1;
	center_y = c2;
}

double Circles::calcArea()
{
	const double PI = 3.14;
	return PI * radius * radius;
}

double Circles::calcCircumference()
{
	const double PI = 3.14;
	return 2 * PI * radius;
}

void Circles::printStats()
{
	cout << "The center of the circle is " << center_x
		<< "   " << center_y << endl;
}

void Circles::setCenter(int x, int y)
{
	center_x = x;
	center_y = y;
}

void Circles::setRadius(double r) {
	radius = r;
}

double Circles::getRadius() const {
	return radius;
}

double Circles::operator-(const Circles &r) const {
	double xval = (r.center_x - center_x)*(r.center_x - center_x);
	double yval = (r.center_y - center_y)*(r.center_x - center_x);

	double distance = sqrt(xval + yval);

	return distance;
}
When one calls a function, it receives copies of the arguments. To fix your situation, pass the parameters by reference:

void operations( Circles& sphere) {

The operations function relies on the menu function being called immediately before it, which isn't ideal. The code could break quite easily if modified. I like to put the switch and menu function call inside a while loop controlled with a boolean Quit:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
bool Quit = false;

while (!Quit) {
   menu(); // with quit option
   // get input
   switch(input) {
      // cases
    4:  // Quit case
        Quit = true;
        break;
    default:
     // default behaviour
    } // end switch

   } // end while 


You have the PI const in there twice. You could make it global, but a better practise is to put it in a header file. constexpr is stronger than const, so use that if you can.

Use const wherever you can, especially for function parameters:

Circles::Circles(const double r, const int c1, const int c2) { // why are the centre values int?

Finally I like to put digits before and after the decimal point to reinforce the idea that it is a double:

2.0 * PI * radius;

It can help avoid integer math especially with division, and it at least saves the compiler an implicit conversion from integer to double.
Last edited on
Thanks for the help.

And the advice is appreciated, thank you!
Topic archived. No new replies allowed.