(solved)please correct my sourcecode.

my answers are wrong. dont know what is wrong with the code though there are no build errors.



#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
//declarations
int operation;
double CirRadius, CirArea, Base, Height, TriArea;
const double PI = 3.14159;

// menu
cin >> operation;
while (operation !=4)
{
if (operation == 1)
{
cin >> CirRadius;
CirArea = PI * CirRadius * CirRadius;
cout<<"The area of a circle with radius "<<CirRadius<<" is " <<setprecision(4)<<CirArea<<'\n\n';
cin >> operation;
}
else if (operation == 2 || operation == 3)
{
cin >> Base;
cin >> Height;
TriArea = 0.5 * Base * Height;
cout<<"The area of a triangle with base "<<Base<<" and height "<<Height<<" is "<<TriArea<<'\n\n';
cin >> operation;
}
else if (operation == 5 || operation == 7)
{
cout<<"Error: "<<operation<<" is not a valid operation.\n\n";

}

}

return 0;
}

Last edited on
Actually your code produces the correct results, but the code itself is not very well organized... I have made a small example of mine:

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
#include <iostream>

using namespace std;

const double PI = 3.14159;


void print_menu()
{
	cout << "1. Circle area" << endl;
	cout << "2. Triangle area" << endl;
	cout << "9. Exit" << endl;
}

void circle_area()
{
	double circle_area;

	cout << "Area of the circle: ";
	cin >> circle_area;
	cout << "The area of the circle is " << PI * (circle_area * circle_area) << endl;
}

void triangle_area()
{
	double triangle_base, triangle_height;

	cout << "Triangle base: ";
	cin >> triangle_base;
	cout << "Triangle height: ";
	cin >> triangle_height;

	cout << "The area of the triangle is " << (triangle_base * triangle_height) / 2 << endl;
}

void menu_opt()
{
	char opt = 0;

	while(opt != '9')
	{
		print_menu();

		cout << ">> ";
		cin >> opt;

		switch(opt)
		{
		case '1':
			circle_area();
			break;
		case '2':
			triangle_area();
			break;
		case '9':
			return;
		default:
			cout << "Invalid option!" << endl;
		}
	}
}

int main()
{
	menu_opt();

	cout << "Press <Enter> to exit..." << endl;
	
	cin.ignore(); //To flush input stream...
	cin.get();

	return 0;
}
thanks outsid3r. i've organized the program better now and my output is just fine. thanks again
Topic archived. No new replies allowed.