Need help with my C++ Structure Circle code

Working on this code for homework.
Instructions here:
Structures Lab (15 pts): Write a small C++ program that uses C++ structures. The program will deal with a
structure called Circle that contains the various attributes of a circle. It will create a Circle structure variable, display
the Circle number, then use functions to: get the circle data, calculate the circle data, and finally display the data.
Allow users to get as many circles as wanted until the user indicates otherwise.
Required Functions. Your C++ program needs the following 4 functions.
1. Prototype: Circle getCircleData(); This function prompts for a radius. Accept only values greater than 0. It
returns a Circle structure variable.
2. Prototype: void calculateCircleData(Circle &); This function takes the Circle reference argument and
calculates the rest of the attributes, diameter, circumference, and area.
3. Prototype: void displayCircleData(Circle); This function takes the Circle argument and displays all of the
attributes. Displays the radius, diameter, area and circumference, all with 2 decimal places.
4. Prototype: bool tryAgain(); This function asks the user if they want to try again and get another Circle. If
they do, then return true, otherwise return false. You will use this function in a loop to allow users to get as
many Circles as they want. do{ … } while (tryAgain( ) ) ;

So far I've gotten the code to run with no errors, I just can't get my head around why the data being displayed is only 0.0. I'm sure it's got to be a simple fix because I'm having trouble focusing :L

Here is my code thus far:
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
#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>

using namespace std;


//structure declarations
struct Circle
{
	double radius;
	double diameter;
	double area;
	double circumference;

//default constructor here
Circle()
{
	radius = 0;
	diameter = 0;
	area = 0;
	circumference = 0;
}
	 
};     //struct Circle

//function prototypes here
Circle getCircleData();
void calculateCircleData(Circle &);
void displayCircleData(Circle);
bool tryAgain();


int main()
{
		Circle circle;
		int number = 0;
	do
	{
		number++;

		cout << "Welcome to our Circle Calculator\n";
		cout << "--------------------------------";
		cout << endl;
		cout << "Circle #" << number << endl;

		getCircleData();
		calculateCircleData(circle);
		displayCircleData(circle);

	}while (tryAgain());

	
}
//function implementation here
Circle getCircleData()
{
	Circle circle;

	cout << "Enter the radius of the circle (number greater than 0): ";
	cin >> circle.radius;

	while (circle.radius < 0)
	{
		cout << "Not a valid input, please enter a valid input (number greater than 0): ";
		cin >> circle.radius;
	}

	return circle;
}

void calculateCircleData(Circle &)
{
	Circle circle;

	circle.diameter = (circle.radius * 2);
	circle.area = (3.1416 * pow(circle.radius, 2));
	circle.circumference = (2 * 3.1416 * circle.radius);
}

void displayCircleData(Circle)
{
	Circle circle;

	cout << fixed << setprecision(2);
	cout << endl;
	cout << "The circle data is \n";
	cout << "Radius: " << setw (6) << circle.radius << endl;
	cout << "Diameter: " << setw (6) << circle.diameter << endl;
	cout << "Circumference: " << setw (6) << circle.circumference << endl;
	cout << "Area: " << setw (6) << circle.area << endl;
}

bool tryAgain()
{
	char again;
	cout << "Do you want to try again? (Y or N) ";
	cin >> again;

	if (again == 'Y' || again == 'y')
	{
		return true;
	}
	return false;
}

And my output when I try to run it:
Welcome to our Circle Calculator
--------------------------------
Circle #1
Enter the radius of the circle (number greater than 0): 7

The circle data is
Radius:   0.00
Diameter:   0.00
Circumference:   0.00
Area:   0.00
Do you want to try again? (Y or N)


If you could please point me in the right direction as to what I need to change to get my output displaying the correct values, I would be very thankful :)

I have consistently found help searching through threads here, first time I needed help past searching o.O So it's nice to finally make an account and post. Also thank you for the help ahead of time.
Last edited on
Your display function is where the problem lies. You did not pass a new circle object to the function. Instead you created a new one and displayed it's values which looks right from the way you have done your constructor. To be able to display the values of the circle you created in main, you must change your display function to something like this:

1
2
3
4
5
6
7
8
9
10
void displayCircleData(Circle& circle)
{
	cout << fixed << setprecision(2);
	cout << endl;
	cout << "The circle data is \n";
	cout << "Radius: " << setw (6) << circle.radius << endl;
	cout << "Diameter: " << setw (6) << circle.diameter << endl;
	cout << "Circumference: " << setw (6) << circle.circumference << endl;
	cout << "Area: " << setw (6) << circle.area << endl;
}


E: Just took another look at your other functions and you seem to be doing the samething as well. Why are you creating new objects inside the functions and doing one thing with them and leaving them. The problem started from your main function.

Your getCircledata function prototype should be:
Circle& getCircleData();

And should return a pointer to circle
Line 48 should be:
circle = getCircleData();

Remove the newly created Circle objects in functions:
DisplayCircle() and CalculateCircleData()

Last edited on
Okay I see why that would cause the values to show up like they did. So after I changed the display function parameters, I got some errors regarding where I called the function in the main. I'm not quite sure what I would change the parameters to when I call it.

I left the call function as displayCircleData(circle), but I've tried changing the parameters to multiple things, just not getting it :l

Here are the errors I'm getting:

Error 1 error LNK2019: unresolved external symbol "void __cdecl displayCircleData(struct Circle)" (?displayCircleData@@YAXUCircle@@@Z) referenced in function _main c:\Users\Jacob\documents\visual studio 2010\Projects\Ch 7 Program 1 - Structures Lab\Ch 7 Program 1 - Structures Lab\Ch 7 Program 1 - Structures Lab.obj
Error 2 error LNK1120: 1 unresolved externals c:\users\jacob\documents\visual studio 2010\Projects\Ch 7 Program 1 - Structures Lab\Debug\Ch 7 Program 1 - Structures Lab.exe 1
Error 3 IntelliSense: more than one instance of overloaded function "displayCircleData" matches the argument list: c:\users\jacob\desktop\ch 7 program 1 - structures lab.cpp 60

Edit: Read your edit and changed some things around, got it to run with just a warning and no errors. Just trying to figure out why it's order is all off now.


Last edited on
I'm stuck.

I did to the best of my knowledge what you said, but I still get tiny errors and the values don't show up when I can get around the errors. Sorry if this is a blatantly easy fix, I'm still a newbie at C++.

I just removed any arguments from the displayCircleData() function call because it wasn't accepting any I tried.

Here is my code thus far:
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
#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>

using namespace std;


//structure declarations
struct Circle
{
	double radius;
	double diameter;
	double area;
	double circumference;

//default constructor here
Circle()
{
	radius = 0;
	diameter = 0;
	area = 0;
	circumference = 0;
}
	 
};     //struct Circle

//function prototypes here
Circle& getCircleData();
void calculateCircleData(Circle &);
void displayCircleData(Circle);
bool tryAgain();


int main()
{
		Circle circle;
		circle = getCircleData();
		int number = 0;
	do
	{
		number++;

		cout << "Welcome to our Circle Calculator\n";
		cout << "--------------------------------";
		cout << endl;
		cout << "Circle #" << number << endl;

		getCircleData();
		calculateCircleData(circle);
		displayCircleData();

	}while (tryAgain());

	
}
//function implementation here
Circle& getCircleData()
{
	Circle circle;

	cout << "Enter the radius of the circle (number greater than 0): ";
	cin >> circle.radius;

	while (circle.radius < 0)
	{
		cout << "Not a valid input, please enter a valid input (number greater than 0): ";
		cin >> circle.radius;
	}

	return circle;
}

void calculateCircleData(Circle& circle)
{
	circle.diameter = (circle.radius * 2);
	circle.area = (3.1416 * pow(circle.radius, 2));
	circle.circumference = (2 * 3.1416 * circle.radius);
}

void displayCircleData(Circle& circle)
{
	cout << fixed << setprecision(2);
	cout << endl;
	cout << "The circle data is \n";
	cout << "Radius: " << setw (6) << circle.radius << endl;
	cout << "Diameter: " << setw (6) << circle.diameter << endl;
	cout << "Circumference: " << setw (6) << circle.circumference << endl;
	cout << "Area: " << setw (6) << circle.area << endl;
}

bool tryAgain()
{
	char again;
	cout << "Do you want to try again? (Y or N) ";
	cin >> again;

	if (again == 'Y' || again == 'y')
	{
		return true;
	}
	return false;
}


Here are the errors/warnings:

Error 1 error C2660: 'displayCircleData' : function does not take 0 arguments c:\users\jacob\desktop\ch 7 program 1 - structures lab.cpp 61
Warning2 warning C4172: returning address of local variable or temporary c:\users\jacob\desktop\ch 7 program 1 - structures lab.cpp 81
Error 3 IntelliSense: no instance of overloaded function "displayCircleData" matches the argument list c:\users\jacob\desktop\ch 7 program 1 - structures lab.cpp 61
Error 4 IntelliSense: too few arguments in function call c:\users\jacob\desktop\ch 7 program 1 - structures lab.cpp 61

It seems the only place it has issues is the function call, but I'm not sure if there's something else I missed.
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 <cmath>
#include <iomanip>
#include <string>

using namespace std;


//structure declarations
struct Circle
{
	double radius;
	double diameter;
	double area;
	double circumference;
    
    //default constructor here
    Circle()
    :radius(0), diameter(0), area(0), circumference(0) {}
    
};     //struct Circle

//function prototypes here
Circle& getCircleData();
void calculateCircleData(Circle &);
void displayCircleData(Circle&);
bool tryAgain();


int main()
{
    Circle circle;
    int number = 0;
	do
	{
		number++;
        
		cout << "Welcome to our Circle Calculator\n";
		cout << "--------------------------------";
		cout << endl;
		cout << "Circle #" << number << endl;
        circle = getCircleData();
		calculateCircleData(circle);
		displayCircleData(circle);
        
	}while (tryAgain());
    
	
}
//function implementation here
Circle& getCircleData()
{
	Circle* circle = new Circle;
    
	cout << "Enter the radius of the circle (number greater than 0): ";
	cin >> (*circle).radius;
    
	while ((*circle).radius < 0)
	{
		cout << "Not a valid input, please enter a valid input (number greater than 0): ";
		cin >> (*circle).radius;
	}
    
	return *circle;
}

void calculateCircleData(Circle& circle)
{
	circle.diameter = (circle.radius * 2);
	circle.area = (3.1416 * pow(circle.radius, 2));
	circle.circumference = (2 * 3.1416 * circle.radius);
}

void displayCircleData(Circle& circle)
{
	cout << fixed << setprecision(2);
	cout << endl;
	cout << "The circle data is \n";
	cout << "Radius: " << setw (6) << circle.radius << endl;
	cout << "Diameter: " << setw (6) << circle.diameter << endl;
	cout << "Circumference: " << setw (6) << circle.circumference << endl;
	cout << "Area: " << setw (6) << circle.area << endl;
}

bool tryAgain()
{
	char again;
	cout << "Do you want to try again? (Y or N) ";
	cin >> again;

	return ((again|0x20) == 'y');
}
Thank you so much for your help :)

E: One thing before I mark this as solved (again), with my code it looks quite different than yours and I get this error:

Error 1 error C2664: 'calculateCircleData' : cannot convert parameter 1 from 'Circle' to 'Circle *' c:\users\jacob\desktop\ch 7 program 1 - structures lab.cpp 58

Is there something simple I can do to fix this to make it run, or does this 1 error lead to many more changes, just a quick answer will do thanks :) The only reason I ask is because my professor wants our code how the instructions say, and your code is not (even though it works).

Code:
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
#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>

using namespace std;


//structure declarations
struct Circle
{
	double radius;
	double diameter;
	double area;
	double circumference;

//default constructor here
Circle()
{
	radius = 0;
	diameter = 0;
	area = 0;
	circumference = 0;
}
	 
};     //struct Circle

//function prototypes here
Circle getCircleData();
void calculateCircleData(Circle *data);
void displayCircleData(Circle display);
bool tryAgain();


int main()
{
		Circle data;
		int number = 0;
	do
	{
		number++;
		cout << "Welcome to our Circle Calculator\n";
		cout << "--------------------------------";
		cout << endl;
		cout << "Circle #" << number << endl;

		data = getCircleData();
		calculateCircleData(data);
		displayCircleData(data);

	}while (tryAgain());

	return 0;
}
//function implementation here
Circle getCircleData()
{
	Circle r;

	cout << "Enter the radius of the circle (number greater than 0): ";
	cin >> r.radius;

	while (r.radius < 0)
	{
		cout << "Not a valid input, please enter a valid input (number greater than 0): ";
		cin >> r.radius;
	}

	return r;
}

void calculateCircleData(Circle &data)
{
	data.diameter = (data.radius * 2);
	data.area = (3.1416 * pow(data.radius, 2));
	data.circumference = (2 * 3.1416 * data.radius);
}

void displayCircleData(Circle display)
{
	
	cout << fixed << showpoint << setprecision(2);

	cout << "The circle data is \n";
	cout << "Radius: " << setw (6) << display.radius << endl;
	cout << "Diameter: " << setw (6) << display.diameter << endl;
	cout << "Circumference: " << setw (6) << display.circumference << endl;
	cout << "Area: " << setw (6) << display.area << endl;
}

bool tryAgain()
{
	char again;
	cout << "Do you want to try again? (Y or N) ";
	cin >> again;
	cout << endl;

	if (again == 'Y' || again == 'y')
	{
		return true;
	}
	return false;
}
Last edited on
Umm...you totally just went back to where you were before I posted a working solution. Can you please look at what you have and I what I posted and spot the differences yourself?

P.s. it would be wise if you decide you want to deal with pointers or just references. Doing both gets confusing at times
I was going to edit my post before you posted :L

I did fix it myself.. I didn't understand parts of the code you had so therefore I wasn't going to use it exactly, but I did find some help on structures and changed my code some more.

I got it to work by using these as my prototype arguments:
1
2
3
4
Circle getCircleData();
void calculateCircleData(Circle &data);
void displayCircleData(Circle display);
bool tryAgain();


And from there I just interchanged the variables I had in the functions.

Thank you for your help Smac89.
Topic archived. No new replies allowed.