Reputation: 653
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?
Upvotes: 1
Views: 46845
Reputation: 1
#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++
Upvotes: 0
Reputation: 1
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;
Upvotes: 0
Reputation: 25487
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."
Upvotes: 0
Reputation: 15780
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";
Upvotes: 1
Reputation: 1055
It's '\t' not '/t'.
it should be:
<< "\tRadius=" << radius << "\n"
<< "\tArea=" << area << "\n"
<< "\tCircumference=" << circumference << "\n";
Upvotes: 0
Reputation: 454912
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.
Upvotes: 0