开发者

Calculate Area and Circumference of a Circle with input in C++

开发者 https://www.devze.com 2023-01-18 08:01 出处:网络
I am writing a code for class and I thought I hat it all down pat. However, can not get any feedback from the input when I compile this on XCode.

I am writing a code for class and I thought I hat it all down pat. However, can not get any feedback from the input when I compile this on XCode.

My code is as follows:

/*
 James George Hayek
 PCCC Assignment 2
 Prof Siegel

 This program will calculate the area and circumference of a circle.
 */

#include <iostream>
#include <cmath>
using namespace std;

int main () 
{

    float radius;
    float circumference;
    float area;

    cout << "Please enter the radius of a circle: ";
    cin >> radius;
    cout << "\n";

    circumference = 2 * 3.1416 * radius;
    area = 3.1416 * radius * radius;

    cout << "************************************" << "\n"
         << "*Area and Circumference of A Circle*" << "\n"
         << "************************************" << "\n"
         << "\tRadius= " << radius << "\n"
         << "\tArea= " << area << "\n"
         << "\tCircumference= " << circumference << "\n";

    cin.get();


    return 0;

} //end main

Okay, I just trouble开发者_开发技巧 shot this and it seems as though this works in the terminal but I can not get it to respond in the console in XCode. Should I not worry about this?


You got your tab back wards on your output: /t should be \t. I don't see anything else wrong with the code. (tested)

cout << "************************************" << "\n"
      << "*Area and Circumference of A Circle*" << "\n"
      << "************************************" << "\n"
      << "\tRadius=" << radius << "\n"
      << "\tArea=" << area << "\n"
      << "\tCircumference=" << circumference << "\n";


Your program compiles and runs fine but its not pretty printing the result because you need \t to get a tab printed and you've used /t.

\t is a single tab char where as /t is two separate char / and a t

Here is the list of escape sequences available.


It's '\t' not '/t'.

it should be:

 << "\tRadius=" << radius << "\n"
 << "\tArea=" << area << "\n"
 << "\tCircumference=" << circumference << "\n";


Other posts have pointed out the reason/fix.

Just noticed:

Your code silently loses precision in the statements as there is a implicit double to float standard conversion that applies behind your back:

circumference = 2 * 3.1416 * radius;  (since 3.1416 is of type double, 
                                       the type of rhs is double).
area = 3.1416 * radius * radius;      (same here)

So, you lose precison. Don't do it unless it is intentional. Compile with the strictest warning level though that is not guaranteed to bring out all lurking issues.

$5/10 - "Otherwise, if either operand is double, the other shall be converted to double."


Area of Circle = (Radius * Radius) * PI

function area_of_circle($radius){

$area = pow($radius, 2) * pi();

return $area; }

// given value $radius = 2;

$area = area_of_circle($radius);

echo "Radius of Circle : ".$radius." "; echo "Area of Circle : ".$area;

download here


#include<stdio.h>
#include<conio.h>
void main()
{

float radius,area;
clrscr();  // Clear Screen

printf("nEnter the radius of Circle : ");
scanf("%d",&radius);

area = 3.14 * radius * radius;

printf("nArea of Circle : %f",area);
getch();

}

i already translated the code from PHP to C++

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号