Hello All,

I am still pretty new to C++ and am just trying to create simple codes to strengthen my skills. I have written the code below that is supposed to allow the user to pick a shape (either circle, square, or rectangle) and then input either the radius or sides and receive the area. The area for the square and the rectangle is working correctly, but when I choose circle and put in any radius, it gives me an area of 0. Can anybody please help me get some insight into why the formula for area of a circle isn't working?

``````#include <iostream>
#include <cctype>

using namespace std;

void displayInstructions(void);
char userShape(void);
double areaCircle(double radius);
double areaSquare(double side);
double areaRect(double side1, double side2);
void displayArea(double area);

int main(void)
{
displayInstructions();

char choiceShape;
double radius=0,side=0,side1=0,side2=0,areaFinal=0;

choiceShape = userShape();

switch(choiceShape)
{
case 'c':
cout << "\nPlease enter the radius of the circle: ";
cin >> radius;

areaFinal = areaCircle(radius);

displayArea(areaFinal);
break;
case 's':
cout << "\nPlease enter the size of a side of the square: ";
cin >> side;

areaFinal = areaSquare(side);

displayArea(areaFinal);
break;
case 'r':
cout << "\nPlease enter size of the first side\n"
<< "of the rectangle: ";
cin >> side1;
cout << "\nPlease enter the size of the second side\n"
<< "of the rectangle: ";
cin >> side2;

areaFinal = areaRect(side1,side2);

displayArea(areaFinal);
break;
default:
cout << "\nYou did not enter c, s, or r.  Sorry!"
<< endl;
break;
}
return 0;
}

void displayInstructions(void)
{
cout << "This program computes the area of either a\n"
<< "circle, square, or rectangle based ont he user's\n"
<< "input.\n"
<< endl;
}

char userShape(void)
{
char shape;
cout << "Please enter your shape (c = Circle, s = Square\n"
<< "and r = Rectange): ";
cin >> shape;

while(!isalpha(shape))
{
cout << "\nYou did not enter a letter.\n"
<< "Please enter your shape (c = Circle, s = Square\n"
<< "and r = Rectange): ";
cin >> shape;
}

return shape;
}

double areaCircle(double radius)
{
const double PI = 3.141592;
double totalArea = 0;
totalArea = (1/2) * PI * (radius*radius);
return totalArea;
}

double areaSquare(double side)
{
double totalArea = 0;
totalArea = side*side;
return totalArea;
}

double areaRect(double side1, double side2)
{
double totalArea = 0;
totalArea = side1*side2;
return totalArea;
}

void displayArea(double area)
{
cout << "\nThe area of your shape is: " << area << endl;
cin.get();
}``````

Thanks for your help!

-D

So, apparently, this works if I change the (1/2) to .5. Is there any reason why this should make it work? Also, if it is the case that fractions need to be converted, what happens if I have a fraction with an extremely long decimal?

-D

So, apparently, this works if I change the (1/2) to .5. Is there any reason why this should make it work?

To the compiler, `1` and `2` look like ints. Integer division truncates, so `1/2` is zero. Try `1.0/2` , or `1/2.0` .

To the compiler, `1` and `2` look like ints. Integer division truncates, so `1/2` is zero. Try `1.0/2` , or `1/2.0` .

Thank you! I was so confused as to why that wasn't working and that makes much more sense. I have marked this thread as solved. Sorry if that was a stupid questions. Thanks again.

-D

``totalArea = (1/2) * PI * (radius*radius);``

what you are calculating is the area of a half of a circle.

The area of a circle is pi* r^2