C++ Homework?? (Area of a Shaded Region)

Pages: 12
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.

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
#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;
}
}
Last edited on
Use code tags and point out what lines 22, 24 and 26 are.

To use code tags, put your code between these: [code][/code]
there. now what did I do wrong? This is due tomorrow night. :/
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

It should look like this:

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
#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;
}
Last edited on
That gets some of that out of the way now i still got:


Area2.cpp:21: no match for `ostream & < ostream & ()(ostream &)'
Area2.cpp:21: candidates are: operator <(ostream & (*)(ostream &), ostream & (*)(ostream &)) <builtin>
Area2.cpp:21: operator <(void *, void *) <builtin>
Area2.cpp:23: no match for `ostream & < ostream & ()(ostream &)'
Area2.cpp:23: candidates are: operator <(ostream & (*)(ostream &), ostream & (*)(ostream &)) <builtin>
Area2.cpp:23: operator <(void *, void *) <builtin>
Area2.cpp:25: no match for `ostream & < ostream & ()(ostream &)'
Area2.cpp:25: candidates are: operator <(ostream & (*)(ostream &), ostream & (*)(ostream &)) <builtin>
Area2.cpp:25: operator <(void *, void *) <builtin>
Area2.cpp:27: parse error at end of input
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.

Something like this:
squarearea = (2 * radius * 2 * radius);
nothing changed
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?
The errors are for the 3 cout lines and the end
Could it be that you have <std::endl; instead of << std::endl;?
Last edited on
Haha, yea can't believe I didn't notice that. Although the space I don't think should matter, but << instead of < obviously does.
yep. now it's just the "parse error at end of input" in line 27
You're missing a curly bracket, the one you have there closes the if statement, not main().
I think your if statement is missing a }
Now it runs.
It does:

Hit any key to start. Hit key
Enter a Radius Type number
Hit any key to continue Hit key

Exits
We'll have to see the new code with [code][/code] tags, please.
updated the 1st post
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:

1
2
return 0;
//end if radius <0   <--- brace goes here 
ok. i put a brace in line 19 and get
Area2.cpp:27: parse error before `}'
Now you have too many braces. Remove one of the last ones.
Pages: 12