6

Here's the code.

int a;
int pi = 3.14;
int area;
int main()
{
    cout << "Input the radius of the circle ";
    cin >> a;

    a *= a *= pi >> area;

    cout << "The area is " << area;


}
Matt Bettinson
  • 533
  • 3
  • 8
  • 22
  • 14
    3 is a fairly inaccurate estimate for pi. – CB Bailey Jan 15 '11 at 15:55
  • 5
    This code is so broken it's not even funny any more. For starters: Do you realize that `cin >> ...` (and `cout << ...`) is a very special case and `<<` (and `>>`) mean something very different in about every other context, including in `pi << area`? You're lucky you get 0 though, you might as well get anything. –  Jan 15 '11 at 15:56
  • 1
    Please see here: http://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points . `a *= a *= pi >> area;` has _undefined behavior_. – CB Bailey Jan 15 '11 at 15:57
  • Does that even compile? I though double isn't implicitly convertible to int. – CodesInChaos Jan 15 '11 at 15:59
  • 2
    @CodeInChaos: Yes, any floating point type may legally be converted to any integer type. The behaviour is only undefined if the truncated value cannot be represented in the destination type. – CB Bailey Jan 15 '11 at 16:01
  • @Charles: Yet an error of about 0.1416 is hardly responsible for the wrong result here ;-) – fredoverflow Jan 15 '11 at 16:02
  • As a buddy of mine would say...too clever by 1/2. a = ( a * a ) * pi; – kenny Jan 15 '11 at 16:03
  • 5
    @Matt Bettinson: This code shows that you don't yet have a good grasp of some of the fundamentals of C++, I suggest that you get a good beginners book and work through the first few chapters. http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – CB Bailey Jan 15 '11 at 16:03
  • @FredOverflow: Hence a comment; not an answer. – CB Bailey Jan 15 '11 at 16:04
  • @delnan: Actually, globals are guaranteed to be initialized to 0. (but there's no good reason to be using global variables here either) – Ben Voigt Jan 15 '11 at 16:37
  • I wonder if C++ is not the first OP's programming language. If so, could you name it? – ulidtko Jan 15 '11 at 17:41

8 Answers8

13

The >> operator when used with numbers is right shift, not assignment. You want something like

area = a * a * pi;

Update

You also need to use a floating point type or your answer won't be what you expect.

float a;
float pi = 3.14f;
float area;
Alex
  • 2,152
  • 2
  • 17
  • 20
7

I don't have enough patience to decipher your strange code. How about just area = a * a * pi?

fredoverflow
  • 256,549
  • 94
  • 388
  • 662
6

Your code doesn't make any sense.

pi(and all your other variables) need to be double or float,... not int. An int can only contain an integral number. And pi is obviously not integral.

a *= a *= pi >> area; should be area = a * a * pi;

>> is a bitshift, not an assignment to the right side
*= is multiply assign and not just multiply. i.e. it is similar to left=left*right

CodesInChaos
  • 106,488
  • 23
  • 218
  • 262
2

Your code doesn't do what I think you wanted it to do. You don't assign to variables with >>; that is only for stream extraction (and bitshifting).

Also, a *= a *= pi probably doesn't do what you think it does.

Also, you want floating-point values, not int. An "int" pi is just 3.

Also, you should have error checking on your stream extraction!

Try:

int main()
{
    const float pi = 3.14;
    float a;

    cout << "Input the radius of the circle ";
    if (!(cin >> a)) {
         cout << "Invalid radius!";
         return 1;
    }

    float area = (a * a * pi);

    cout << "The area is " << area;
}
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
2
int pi = 3.14;

Wrong datatype. Assigning double value to int? That's wrong.

Write this:

double pi = 3.14;

And likewise, change other datatypes to double as well.

Nawaz
  • 353,942
  • 115
  • 666
  • 851
2

The area of a circle is pi * r * r therefore you would want to do;

a = a * a * pi

Hope that helps

and they all would need to be floats.

Pudgeball
  • 814
  • 8
  • 16
1

Because you're using int, or integer, for all your variables. You want to use doubles or even floats. (doubles are more precise).

Maxpm
  • 24,113
  • 33
  • 111
  • 170
  • I think you were downvoted because it does not cover all problems with the code (e.g. the attempted assignment to `area` using the `>>` operator). – dreamlax Jul 01 '12 at 05:32
1

All your variables are declared as int, which simply drops any fractional portion assigned to it. To work with floating-point values, use double instead.

Also, your equation in almost incomprehensible. Not sure what you're trying to do there.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466