Area of a Triangle

Edit: I figured out how to do everything and it works, but my area for the triangle part keeps giving me values of 0 for every answer.

The program is a Geometry calculator, works perfectly.

Code:

#include <iostream>
#include <iomanip>

using namespace std;

int main()

{

int loop=1;
int choice;

while (loop==1)
{
system("CLS");
cout << "::Geometry Calculator::\n\n"
<< "1. Area of a Circle\n"
<< "2. Area of a Triangle\n"
<< "3. Exit\n\n";
cin >> choice;
switch(choice)
{
case 1:
system("CLS");

double pi, radius, area;
cout << "::Area of a Circle::\n\n"
<< "Enter your radius: ";
cin >> radius;

pi = 3.14159;

area = pi * (radius*radius);

cout << "The area of the circle is " << area << "." << endl;
system("PAUSE");
}
switch(choice)
{
case 2:
if(choice==2)
system("CLS");

int base, height, a2;
cout << "::Area of a Triangle::\n\n"
<< "Enter your base: ";
cin >> base;

cout << "Enter your height: ";
cin >> height;

a2 = 1/2 * base * height;
cout << "Your result is " << a2 << "." << endl;

system("PAUSE");
}
switch(choice)
{
case 3:
if(choice==3)
system("CLS");

cout << "Adios!!\n\n";
system("PAUSE");
exit(0);
}

}
}

If anyone knows why I keep getting zeros for the triangle part, please help.

Thank you!
Last edited on
1/2 is 0.

Try 1.0/2.0 or just (base * height) / 2
(base * height) / 2

This may not always be accurate as base and height are both declared as type int and the magic number 2 is written as an int so if there is a remainder it will be lopped.
He's assigning to an int. The number is already being truncated.
Sorry for the late response, but I was able to get it bout 5 minutes after I posted.

But I did try your way, and it worked the same. Thank you for your help anyways!
Topic archived. No new replies allowed.