tags:

views:

74

answers:

4

Hello again,

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?

A: 

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.

codaddict
Ah ha, I noticed this justtt after I posted the code. Thanks, I made the change.
James Hayek
A: 

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

it should be:

 << "\tRadius=" << radius << "\n"
 << "\tArea=" << area << "\n"
 << "\tCircumference=" << circumference << "\n";
vash47
+1  A: 

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";
Ruel
A: 

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."

Chubsdad
Hmm, I thought float would be enough, I will look into using double precision for this and other codes that require it.
James Hayek
Yeah, good to know about but when you're hardcoding 3.1416 I don't think you need the extra digits ;-).