I gotta make a c++ program that calculates the area of a shaded region when the user types in the radius.
The picture is square with a circle tightly circumscribed within it so each quadrant is touching a midpoint. And the shaded region is between the 2 shapes.
#include <iostream>
// variables
double pi = 3.14159;
double circlearea = 0;
double squarearea = 0;
double result = 0;
double radius = 0; //to be obtained by user
int main ()
{
std::cout << "Enter a Radius";
std::cin >> radius;
if (radius < 0)
{
std::cout << "Negative number!";
return 0;
//end if radius <0
circlearea = (pi * radius * radius);
std::cout << "The area of the circle is:" << circlearea <<std::endl;
squarearea = (2 * radius * 2 * radius);
std::cout << "The area of the square is:" << squarearea <<std::endl;
result = (squarearea - circlearea);
std::cout << "The area of the shaded region is:" << result <<std::endl;
}
}
you don't have ';' at the end of lines 21, 23 and 25. Also you use 'Radius' and 'radius'.
Also you didn't actually do use your code tags correctly. There is a button to the right of the the box that you reply in that looks like <> click that and the tags should come
#include <iostream>
// variables
double pi = 3.14159;
double circlearea = 0;
double squarearea = 0;
double result = 0;
double radius = 0; //to be obtained by user
int main ()
{
std::cout << "Enter a Radius";
std::cin >> radius;
if (radius < 0)
{
std::cout << "Negative number!";
return 0;
//end if radius <0
circlearea = pi * radius * radius;
std::cout << "The area of the circle is:" << circlearea <std::endl;
squarearea = 2 * radius * 2 * radius;
std::cout << "The area of the square is:" << squarearea <std::endl;
result = squarearea - circlearea;
std::cout << "The area of the shaded region is:" << result <std::endl;
}
Maybe try putting the calculations part in brackets. Not sure why that should matter, but it wouldn't be the first time putting things in brackets solved a problem like this.
Hmm... I can't really see if it seems to do circlearea ok why it's not doing squarearea. I take it it is printing out what you expect it to for circlearea?
You're ending the if statement in the wrong place. The area calculations are within the if (radius < 0) statement, not to mention they won't get hit because the program returns before then. You should close the if statement here: