I need to check my code has all these requirements.
The requirements for “main()” are:
a. Use an object.
b. Declare all required variables.
c. Output user information.
d. Input the radius 1 and radius 2 to be used to calculate the area of two circles.
e. Use object Circle to access an array that contains the radius of the two circles.
f. Use a function to display the area of the two circles.
Requirements for the function to display the area of the two circles:
a. The function is a void function.
b. The function receives the Circle object using an array.
c. Use a loop which will access the radius of the two circles in the array.
Output the area of the two circles using the function to calculate the area.

#include <iostream>
#include <iomanip>
#include "Circle.h"
using namespace std;
const int NUM_CIRCLES = 2;
int main()
{
Circle circle[NUM_CIRCLES];

cout<<"Chapter 2 Homework\n\n";

cout<<"\n\n";

for (int index = 0; index < NUM_CIRCLES; index++)
{
double r;
cout <<"Enter radius "<< (index+1) << ": ";
cin >> r;
}

cout << fixed << showpoint << setprecision(1);
cout << "\nHere are the areas of the " << NUM_CIRCLES
<< " circles.\n";
for (int index = 0; index < NUM_CIRCLES; index++)
{
cout << "Circle " << (index+1) << setw(8)
<< circle[index].findArea() << endl;
}
return 0;
}

You need to have a void function that displays the area of the circles in the array. It should be like

void DisplayArea(Circle[NUM_CIRCLES] circles)
{
for (int index = 0; index < NUM_CIRCLES; index++)
{
cout << "Circle " << (index+1) << setw(8)
<< circle[index].findArea() << endl; …

You would create a function like what I have shown. Then instead of having lines 27-31 you would just call that function passing the array.

All 7 Replies

You need to have a void function that displays the area of the circles in the array. It should be like

void DisplayArea(Circle[NUM_CIRCLES] circles)
{
for (int index = 0; index < NUM_CIRCLES; index++)
{
cout << "Circle " << (index+1) << setw(8)
<< circle[index].findArea() << endl;
}
}

Then you main function will get changed to:

//...
cout << "\nHere are the areas of the " << NUM_CIRCLES
<< " circles.\n";
DisplayArea(circles);
return 0;
}

Would this go at the end of the program? How would it look like together?

You would create a function like what I have shown. Then instead of having lines 27-31 you would just call that function passing the array.

// This program uses an array of objects.
// The objects are instances of the Circle class.
#include <iostream>
#include <iomanip>
#include "Circle.h" // Needed to create Circle objects
using namespace std;

int main()
{
const int NUM_CIRCLES = 2;
Circle circle[NUM_CIRCLES];
cout<<"Chapter 2 Homework\n\n";

cout<<"\n\n";

for (int index = 0; index < NUM_CIRCLES; index++)
{
double r;
cout <<"Enter radius "<< (index+1) << ": ";
cin >> r;
}
// Use a loop to get and print out the area of each object
cout << fixed << showpoint << setprecision(2);
void DisplayArea(Circle[NUM_CIRCLES] circle)
{
for (int index = 0; index < NUM_CIRCLES; index++)
{
cout << "Circle " << (index+1) << setw(8)
<< circle[index].findArea() << endl;
}
}

//...
cout << "\nHere are the areas of the " << NUM_CIRCLES
<< " circles.\n";
DisplayArea(circle);
return 0;
}

Is this how my code is supposedd to look like? I get errors. These below I'm sorry I'm new.

\Desktop\spdat\Chapter 2 HW.cpp||In function 'int main()':|
\Desktop\spdat\Chapter 2 HW.cpp|25|error: expected ',' or '...' before 'circle'|
\Desktop\spdat\Chapter 2 HW.cpp|26|error: a function-definition is not allowed here before '{' token|
\Desktop\spdat\Chapter 2 HW.cpp|39|error: expected '}' at end of input|
||=== Build failed: 3 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

``

In C++ you either have to put the functions before Main or use prototypes at the beginning of the file. If this is new to you, you should probably review your instruction materials, as this is fairly basic to C++ programming.

I am looking it over. I just am lost and needed some help.

Move lines 25-32 to be before main()

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.