Geometry Calculator code Conundrum

I am a beginner to programming and taking a c++ class at my local college as part of a career/major change.

I've worked out most of this code for an assignment online but I'm missing key elements in the execution that woefully will not be accepted by visual studio. Any help in troubleshooting this script would be much appreciated. I think I have been staring at this for too long. Thanks!

Respectfully,
Armygun087

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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
 #include <iostream>
#include <iomanip>

using namespace std;

int displayMenu();
double calcAreaCircle(double radius);
double calcAreaRect(double length, double width);
double calcAreaTriangle(double base, double height);

const double Pi = 3.14159;

int main()
{
	int choice;
	double radius, width, length, base, height;
	double menu;
	choice = displayMenu();

	double circ_area;
	circ_area = calcAreaCircle(radius);

	double rect_area;
		rect_area = calcAreaRect(length, width);

	double tri_area;
		tri_area = calcAreaTriangle(base, height);

	switch (choice)
	{
	case 1:
	{
		cout << "Please enter the Radius:" << endl;
		cin >> radius;
	}
	break;

	case 2:
	{
		cout << "Please enter the Length:" << endl;
		cin >> length;
		cout << endl;

		cout << "Please enter the width:" << endl;
		cin >> width;
		cout << endl;
	}
	break;

	case 3:
	{
		cout << "Please enter the Base:" << endl;
		cin >> base;
		cout << endl;

		cout << "Please enter the Height:" << endl;
		cin >> height;
		cout << endl;

	}

	break;

	case 4:
		cout << "Thank you for using the geometry calculator, Bye! " << endl;
		system("pause");
		break;

	}

}

int displayMenu()
{
	int choice;

	cout << "Geometry Calculator" << endl;
	cout << "1. Calculate the Area of a Circle" << endl;
	cout << "2. Calculate the Area of a Rectangle" << endl;
	cout << "3. Calculate the Area of a Triangle" << endl;
	cout << "4. Quit" << endl;
	cin >> choice;


	while (choice < 1 || choice > 4)
	{
		cout << "Invalid selection. Enter 1, 2, 3, or 4: ";
		cin >> choice;
	}
	return choice;

}




double calcAreaCircle(double radius)
{

	double circ_area = Pi * radius*radius;

	if (radius > 0.0)
	{
		cout << "The area of the circle " << radius << " is: " << circ_area << endl;
		system("pause");
		return 0;
	}
	else
	{
		cout << "You did not enter a valid number" << endl;
		cout << "The program will now restart" << endl;
		system("pause");
		return circ_area;
	}
}


double calcAreaRect(double width, double length)
{
	double rect_area = length * width

		if (length > 0.0, width > 0.0);
		{
			cout << "The Area for the Rectangle is: " << rect_area << endl;
			system("pause");
			return 0;
		}

		else
		{
			cout << "You did not enter valid numbers." << endl;
			cout << "The program will now restart." << endl;
			system("pause");
			return rect_area;
		}
}

double calcAreaTriangle(double base, double height)
{
	double tri_area = base * height


		if (base > 0.0, height > 0.0)
		{
			area = 0.5 * base * height;
		}
		else
		{
			cout << "You did not enter a valid number." << endl;
			cout << "The program will now restart." << endl;
			system("pause");
			return tri_area;
		}

}
Hello Armygun087


If you look at line 120 and 140 you will find that these lines are missing the ; at the end of the line. You will also fine that the error messages do not always list the line number with the problem, so you will have to look at what comes before the listed line number. These corrections will also correct some of the other errors that occure because of the missing ;.

You have defined "double menu" in main, but never use this variable. At least this should only be a warning as is with my VS and not a problem to stop the compile.

Line 129 has an "else" without a matching "if" because line 122 ends with a ; and it should not.

Line 144 the variable "area" is undefined.

These are the errors I received along with several warnings. The warnings are not a problem for now and some should go away when the errors are fixed. Others warnings may need some work to fix.

Hope that helps,

Andy

P.S. You should always initialize your variables. Such as in main:
double radius{}, width{}, length{}, base{}, height{};. The {} will initialize the variables to "0.0". As is "width" is being used with no value and the only value it has is the garbage the was at the memory location when the variable was defined.
Last edited on
Hello Armygun087

When I started running your program to see what happens is when I noticed the bigger problems.

At the beginning of the main file you use the line using namespace std;. It is best to learn not to use this line as it WILL get you in trouble some day.

If you are going to use PI as a double you should make use of more than 5 decimal places. You have up to 20 places that you can use.

In main:

You start with defining your variables witch should be initialized at the same time. followed by a call to "displayMenu" which returns a value. so far so good.

Next you define three variables and call three functions passing variables the have no usable (garbage) value, as in your program original code, or have a value of zero if you initialize your variables. This is done before you get to the switch statement where you actually enter a value for the variables that are passed to the functions. I would call it poor program flow.

Line 18 should be followed by line 29 and the "switch" could look something like this:

1
2
3
4
5
6
7
8
9
10
switch (choice)
{
	case 1:
		cout << "Please enter the Radius:" << endl;
		cin >> radius;
		circ_area = calcAreaCircle(radius);
		break;
	default:
		break;
}

witch makes more sense to enter a value into the variable before you call the function. Notice the lack of {}s in the case statement. They are OK if you want to use them, but they are not needed. Everything between the ":" and the ";" at the end of the break statement is considered part of the case statement.

The "displayMenu()" function works. I do not see any problem with this function.

The next three functions kind of work although I am not sure if I understand what you are trying to do with these functions. In all three functions if true you return zero to a variable in main that is never used. if false you return the answer, you consider to be wrong, to the same variable that is never used. The chances are that only the else part will be reached.

In the second and third functions the if statements at lines 122 and 143 are wrong. The ',' operator does not work the way you are thinking. What you need here is the logical && or logical || in the if condition if (length > 0.0 && width > 0.0);. The ";" at the end is not needed here. It shortens the if statement to just this line and everything after this that you want with the "if" is considered separate code. The logical and (&&) is the best choice here because you need both sides to be true.

This is what I have found so far. I still need to do the rearranging I talked about to see if there is anything else.

Hope that helps,

Andy
I am not completely sure I understand, Handy Andy. I was able to fix things so there are no errors now (thank you very much,) but now my code is acting funny.

Here's what I have:
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <iostream>
#include <iomanip>

using namespace std;

int displayMenu();
double calcAreaCircle(double radius);
double calcAreaRect(double length, double width);
double calcAreaTriangle(double base, double height);

const double Pi = 3.14159265359;

int main()
{
	int choice;
	double radius{}, width{}, length{}, base{}, height{};
	double menu;
	choice = displayMenu();

	double circ_area;
	circ_area = calcAreaCircle(radius);

	double rect_area;
		rect_area = calcAreaRect(length, width);

	double tri_area;
		tri_area = calcAreaTriangle(base, height);

	switch (choice)
	{
	case 1:
	{
		cout << "Please enter the Radius:" << endl;
		cin >> radius;
	}
	break;

	case 2:
	{
		cout << "Please enter the Length:" << endl;
		cin >> length;
		cout << endl;

		cout << "Please enter the width:" << endl;
		cin >> width;
		cout << endl;
	}
	break;

	case 3:
	{
		cout << "Please enter the Base:" << endl;
		cin >> base;
		cout << endl;

		cout << "Please enter the Height:" << endl;
		cin >> height;
		cout << endl;

	}

	break;

	case 4:

		cout << "Thank you for using the geometry calculator, Bye! " << endl;
		system ("pause");
		break;

	}

}

int displayMenu()
{
	int choice;

	cout << "Geometry Calculator" << endl;
	cout << "1. Calculate the Area of a Circle" << endl;
	cout << "2. Calculate the Area of a Rectangle" << endl;
	cout << "3. Calculate the Area of a Triangle" << endl;
	cout << "4. Quit" << endl;
	cin >> choice;


	while (choice < 1 || choice > 4)
	{
		cout << "Invalid selection. Enter 1, 2, 3, or 4: ";
		cin >> choice;
	}
	return choice;

}


double calcAreaCircle(double radius)
{

	double circ_area = Pi * radius*radius;

	if (radius > 0.0)
	{
		cout << "The area of the circle " << radius << " is: " << circ_area << endl;
		system("pause");
		return (0);
	}
	else
	{
		cout << "You did not enter a valid number" << endl;
		cout << "The program will now restart" << endl;
		system("pause");
		return circ_area;
	}
}


double calcAreaRect(double width, double length)
{
	double rect_area = length * width;

		if (length > 0.0, width > 0.0)
		{
			cout << "The Area for the Rectangle is: " << rect_area << endl;
			system("pause");
			return 0;
		}

		else
		{
			cout << "You did not enter valid numbers." << endl;
			cout << "The program will now restart." << endl;
			system("pause");
			return rect_area;
		}
}

double calcAreaTriangle(double base, double height)
{
	double tri_area = base * height;


	if (base > 0.0, height > 0.0)
		{
			tri_area = 0.5 * base * height;
		}
		else
		{
			cout << "You did not enter a valid number." << endl;
			cout << "The program will now restart." << endl;
			system("pause");
			return tri_area;
		}

}




What I am getting though is:

Geometry Calculator
1. Calculate the Area of a Circle
2. Calculate the Area of a Rectangle
3. Calculate the Area of a Triangle
4. Quit
1
You did not enter a valid number
The program will now restart
Press any key to continue . . .


None of the values seem to register properly. -_- inputting 1-4 yield the same results unfortunately.
Last edited on
Okay, I've been pouring over this for some time now and am hoping someone can help me fix this so it'll work. I have taken apart most if not all the code, shortened, and removed functions that as a base were making this too complicated to keep. I've gone back to one of my previous projects and hope that this may expedite things. Here's what I have now, but I am getting new error codes. Help is VERY much appreciated. Thank you.

P.S. I know that my established values just below int main may seem redundant but I am beginning to panic. so please excuse, and I apologize for any rash or obvious mistakes where they are.

Respectfully,
Armygun087

-from line one:

"Severity Code Description Project File Line Suppression State
Error LNK2019 unresolved external symbol "double __cdecl calcAreaCircle(double,double)" (?calcAreaCircle@@YANNN@Z) referenced in function_main 1 "

"Severity Code Description Project File Line Suppression State
Error LNK2019 unresolved external symbol "double __cdecl calcAreaRect(double,double)" (?calcAreaRect@@YANNN@Z) referenced in function _main 1"

"Severity Code Description Project File Line Suppression State
Error LNK2019 unresolved external symbol "double __cdecl calcAreaTriangle(double,double)" (?calcAreaTriangle@@YANNN@Z) referenced in function _main 1"

"Severity Code Description Project File Line Suppression State
Error LNK1120 3 unresolved externals 1

- Line 57
"Severity Code Description Project File Line Suppression State
Warning C4551 function call missing argument list 57

-Line 68
"Severity Code Description Project File Line Suppression State
Warning C4551 function call missing argument list 68

And my new 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
#include <iostream>
#include <math.h>

using namespace std;
int main()
{
	int area;
	double radius;
	double width;
	double length;
	double height;
	double calcAreaCircle(const double PI, double radius);
	double calcAreaRect(double length, double width);
	double calcAreaTriangle(double base, double height);
	const double PI = 3.14159265359;

	// Menu //

	cout << "Enter 1 to calculate the area of a circle" << endl;
	cout << "Enter 2 to calculate the area of a rectangle" << endl;
	cout << "Enter 3 to calculate the area of a triangle" << endl;
	cout << "Enter 4 for the program to quit" << endl << "\n";


	while (1 == 1)
	{
		cout << "Choose Your option : ";
		cin >> area;
		cout << endl;

		switch (area)
		{
		case 1:
			cout << "Enter the radius of the circle.\n";
			cin >> radius;
			calcAreaCircle;			//Calculates the area of a cirlce //
			cout << "The area of the circle is " << calcAreaCircle << "\n";
			cout << "Choose Your option : ";
			cin >> area;
			cout << endl;

		case 2:
			cout << "Enter the width of the rectangle.\n";
			cin >> width;
			cout << "Enter the length of the rectangle.\n";
			cin >> length;
			calcAreaRect;			//Calculates the area of a rectangle //
			cout << "The area of the rectangle is " << calcAreaRect << "\n";
			cout << "Choose Your option : ";
			cin >> area;
			cout << endl;

		case 3:
			cout << "Enter the length of triangle's base.\n" << endl;
			cin >> length;
			cout << "Enter the height of the triangle.\n" << endl;
			cin >> height;
			calcAreaTriangle;		//Calculates the area of a triangle //
			cout << "The area of the triangle is " << calcAreaTriangle << "\n";
			cout << "Choose Your option : ";
			cin >> area;
			cout << endl;

		case 4:                                         //Allows user to quit the program //
			cout << "Ending program, standby to make final acknowledgement." << endl;

			system("pause");
			return 0;
		}
	}
}



reopened as new topic Geometry code HELP, Thread closed.
Last edited on
Topic archived. No new replies allowed.