Easy C++ coding

How do I do this
1.Declare the variables double circleRadius (radius of the circle), and double shapeArea (calculated area of the shape).

2.Please use M_PI for the value of PI (approximately 3.14159265), which you get with the math library by inserting these two lines of code at the beginning of your source file (just below the initial comments):

#define _USE_MATH_DEFINES
#include <cmath>

3. Using the cout function in a single command, display the text "Welcome to the Shape Calculator!". Follow this with a line break.

4.Prompt the user to enter the radius of a circle and then store the value in the variable circleRadius and display the calculated area using the formula above. The prompt should state something like "Enter the circle radius:". The result should state something like "The area of the circle is: 42.01", depending on the input provided.

5. Liberally comment the code. Each variable must have a descriptive comment above or on the same line.

The output should look something like this -- user input is "2"
Welcome to the Shape Calculator
Enter the circle radius: 2
The area of the circle is: 12.5664



What I have so far is:
#define _USE_MATH_DEFINES
#include <cmath>

#include <iostream>
using namespace std;

int main ()
{

int circleRadius;
int shapeArea;
const double PI = M_PI;

cout << "Welcome to the Shape Calculator!\n";
cout << "Enter the circle radius:\n";
cout << "The area of the circle is: \n";


return 0;
}


This is using C++
Last edited on
getting there.
const double PI = M_PI; //this is pointless. just use M_PI directly where you need it, other programmers know what this is. Renaming it means you could have given it a value of 11.73 for all they know: they have to figure out what you did and why (trivial in a 1 page program, a huge aggravation in a giant program). Don't be in the habit of doing this to your reader! The instructions are not asking you to do this, just to use the M_PI value.

4) you need a cin statement to read from the user. Once you have that you need to do your computation area = (radius*radius*M_PI) and print that result. I recommend you use doubles here, and possibly for the radius as well.

5) be in the habit of commenting as you go, not saving it up to do later (later never happens in the real world if you do this).

cin looks like this:
cin >> variable;
and a variable in cout looks like this:
cout << "words and stuff " << variable << endl; //note the extra spaces after stuff... to look nice you need to pad spaces around variables in most cases...

cute approach:
double area = M_PI;
area*= radius*radius;

you can also get fancy
area *= pow(radius,2); //showing off

Last edited on
Thank you I will remove the MPI part but I am still confused as to what to do for that outcome. In this specific case would i use
cin >> circleRadius;
and if so is that it? or what needs to follow that?
after you get circleRadius from the user, you need to compute the area and write it out.
I showed you what to do, but one more time:

cin >> circleRadius;
cout "The area of the circle is: " << circleRadius*circleRadius*M_PI << endl; //one way to do it

you could compute the value in a variable. This is useful if you want to do something with it, like use it after to find the volume of a cylinder or whatever else. Just to print it once, you don't need to save it for later.
double area = circleRadius*circleRadius*M_PI;
cout << " ... " << area << endl;

Programming is a series of basic steps, and everything has to be done, the compiler / computer does NOT attempt to guess what you want or anything. You want to write the area from a radius, you have to do each step one by one... get the radius, compute the answer, write the answer out... each step must be in the program explicitly.

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#define _USE_MATH_DEFINES

#include <cmath>
#include <iostream>

int main()
{
   std::cout << "Welcome to the Shape Calculator!\nEnter the circle radius: ";
   double radius { };
   std::cin >> radius;
   std::cout << '\n';

   double area { 2 * M_PI * radius };

   std::cout << "The area of the circle is: " << area << '\n';
}

Welcome to the Shape Calculator!
Enter the circle radius: 2

The area of the circle is: 12.5664


M_PI isn't part of the C or C++ standard. Other non-VS compilers will error out if you don't
#undef __STRICT_ANSI__ .
Last edited on
Topic archived. No new replies allowed.