Working on this code for homework.
Instructions here:
Structures Lab (15 pts): Write a small C++ program that uses C++ structures. The program will deal with a
structure called Circle that contains the various attributes of a circle. It will create a Circle structure variable, display
the Circle number, then use functions to: get the circle data, calculate the circle data, and finally display the data.
Allow users to get as many circles as wanted until the user indicates otherwise.
Required Functions. Your C++ program needs the following 4 functions.
1. Prototype: Circle getCircleData(); This function prompts for a radius. Accept only values greater than 0. It
returns a Circle structure variable.
2. Prototype: void calculateCircleData(Circle &); This function takes the Circle reference argument and
calculates the rest of the attributes, diameter, circumference, and area.
3. Prototype: void displayCircleData(Circle); This function takes the Circle argument and displays all of the
attributes. Displays the radius, diameter, area and circumference, all with 2 decimal places.
4. Prototype: bool tryAgain(); This function asks the user if they want to try again and get another Circle. If
they do, then return true, otherwise return false. You will use this function in a loop to allow users to get as
many Circles as they want. do{ … } while (tryAgain( ) ) ;
So far I've gotten the code to run with no errors, I just can't get my head around why the data being displayed is only 0.0. I'm sure it's got to be a simple fix because I'm having trouble focusing :L
Here is my code thus far:
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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>
using namespace std;
//structure declarations
struct Circle
{
double radius;
double diameter;
double area;
double circumference;
//default constructor here
Circle()
{
radius = 0;
diameter = 0;
area = 0;
circumference = 0;
}
}; //struct Circle
//function prototypes here
Circle getCircleData();
void calculateCircleData(Circle &);
void displayCircleData(Circle);
bool tryAgain();
int main()
{
Circle circle;
int number = 0;
do
{
number++;
cout << "Welcome to our Circle Calculator\n";
cout << "--------------------------------";
cout << endl;
cout << "Circle #" << number << endl;
getCircleData();
calculateCircleData(circle);
displayCircleData(circle);
}while (tryAgain());
}
//function implementation here
Circle getCircleData()
{
Circle circle;
cout << "Enter the radius of the circle (number greater than 0): ";
cin >> circle.radius;
while (circle.radius < 0)
{
cout << "Not a valid input, please enter a valid input (number greater than 0): ";
cin >> circle.radius;
}
return circle;
}
void calculateCircleData(Circle &)
{
Circle circle;
circle.diameter = (circle.radius * 2);
circle.area = (3.1416 * pow(circle.radius, 2));
circle.circumference = (2 * 3.1416 * circle.radius);
}
void displayCircleData(Circle)
{
Circle circle;
cout << fixed << setprecision(2);
cout << endl;
cout << "The circle data is \n";
cout << "Radius: " << setw (6) << circle.radius << endl;
cout << "Diameter: " << setw (6) << circle.diameter << endl;
cout << "Circumference: " << setw (6) << circle.circumference << endl;
cout << "Area: " << setw (6) << circle.area << endl;
}
bool tryAgain()
{
char again;
cout << "Do you want to try again? (Y or N) ";
cin >> again;
if (again == 'Y' || again == 'y')
{
return true;
}
return false;
}
|
And my output when I try to run it:
Welcome to our Circle Calculator
--------------------------------
Circle #1
Enter the radius of the circle (number greater than 0): 7
The circle data is
Radius: 0.00
Diameter: 0.00
Circumference: 0.00
Area: 0.00
Do you want to try again? (Y or N) |
If you could please point me in the right direction as to what I need to change to get my output displaying the correct values, I would be very thankful :)
I have consistently found help searching through threads here, first time I needed help past searching o.O So it's nice to finally make an account and post. Also thank you for the help ahead of time.