calculating the area of a circle problems

Discussion in 'C++' started by jdcrief, Aug 13, 2006.

  1. jdcrief

    jdcrief Guest

    Complier: Visual C++ 2005 Express Edition

    The program I wrote will compile and execute, but the output is always
    the same, no matter what number is entered in for the radius of the
    circle. Someone please help! I am extremely new at this, as you can
    probably tell. What am I missing with the code below?



    #include "stdafx.h"
    #include <iostream>
    #include <cmath>
    using std::cout;
    using std::cin;
    using std::endl;

    int main() {
    const float Pi = 3.14159;
    radius = 0;

    cout << "Enter the radius of a circle: ";
    cin >> radius;

    int area = Pi (radius * radius);
    cout << endl
    << "Area of the circle is = "
    << radius
    << endl;
    return 0;
    }
     
    jdcrief, Aug 13, 2006
    #1
    1. Advertising

  2. jdcrief

    Kaz Kylheku Guest

    jdcrief wrote:
    > Complier: Visual C++ 2005 Express Edition
    >
    > The program I wrote will compile and execute, but the output is always


    It will?

    > the same, no matter what number is entered in for the radius of the
    > circle. Someone please help! I am extremely new at this, as you can
    > probably tell. What am I missing with the code below?
    >
    >
    >
    > #include "stdafx.h"


    This piece of nonsense is unnecessary. It's simply part of a custom for
    handling precompiled headers in AFX projects. AFX stands for
    "application frameworks". the former namer of MFC, or Microsoft
    Foundation Classes. There is no reason for AFX-anything to appear in
    C++ code that doesn't use MFC.

    > #include <iostream>
    > #include <cmath>
    > using std::cout;
    > using std::cin;
    > using std::endl;
    >
    > int main() {
    > const float Pi = 3.14159;
    > radius = 0;


    This compiles???

    Here you are assigning to radius, which you have not defined anywhere.
    Your program is ill-formed.

    It requires a diagnostic message.

    > cout << "Enter the radius of a circle: ";
    > cin >> radius;
    >
    > int area = Pi (radius * radius);


    Here you care trying to call Pi as a function, but you declared it as a
    float. A diagnostic is required.

    I don't think that the compiler you are using will compile this
    program.

    I suspect that you keep running some previous version of the program
    that did compile, without noticing that the new version of the source
    code isn't compiling.

    Try compiling it again and pay attention to the messages.

    > cout << endl
    > << "Area of the circle is = "
    > << radius
    > << endl;


    The area of the circle is its radius? You are not printing the correct
    variable.
     
    Kaz Kylheku, Aug 13, 2006
    #2
    1. Advertising

  3. jdcrief

    osmium Guest

    "jdcrief" writes:

    > Complier: Visual C++ 2005 Express Edition
    >
    > The program I wrote will compile and execute, but the output is always
    > the same, no matter what number is entered in for the radius of the
    > circle. Someone please help! I am extremely new at this, as you can
    > probably tell. What am I missing with the code below?
    >
    >
    >
    > #include "stdafx.h"
    > #include <iostream>
    > #include <cmath>
    > using std::cout;
    > using std::cin;
    > using std::endl;
    >
    > int main() {
    > const float Pi = 3.14159;
    > radius = 0;
    >
    > cout << "Enter the radius of a circle: ";
    > cin >> radius;
    >
    > int area = Pi (radius * radius);


    That's an implicit multiply, computers aren't that intuitive; make it
    explicit.

    int area = Pi*(radius*radius);

    There is no need for parens, but if you want them there is no problem using
    them.

    > cout << endl
    > << "Area of the circle is = "
    > << radius
    > << endl;
    > return 0;
    > }
    >
     
    osmium, Aug 13, 2006
    #3
  4. jdcrief posted:

    > What am I missing with the code below?
    >
    > #include "stdafx.h"
    > #include <iostream>
    > #include <cmath>
    > using std::cout;
    > using std::cin;
    > using std::endl;
    >
    > int main() {
    > const float Pi = 3.14159;
    > radius = 0;



    The object known as "radius" was never defined.


    > cout << "Enter the radius of a circle: ";
    > cin >> radius;
    >
    > int area = Pi (radius * radius);



    Those parentheses might mean multiplication in normal mathematics, but not
    in C++. Furthermore, you're using a signed integer type (and I presume your
    "area" should never be negative).

    unsigned area = pi*radius*radius;

    --

    Frederick Gotham
     
    Frederick Gotham, Aug 13, 2006
    #4
  5. jdcrief schrieb:
    > Complier: Visual C++ 2005 Express Edition
    >
    > The program I wrote will compile and execute,


    Never.

    > but the output is always
    > the same, no matter what number is entered in for the radius of the
    > circle. Someone please help! I am extremely new at this, as you can
    > probably tell. What am I missing with the code below?


    Fix the bugs, then come again. And tell us what output you get.

    > #include "stdafx.h"


    Remove that, unneccessary and non-standard.

    > #include <iostream>
    > #include <cmath>
    > using std::cout;
    > using std::cin;
    > using std::endl;
    >
    > int main() {
    > const float Pi = 3.14159;
    > radius = 0;


    radius ist undeclared.

    > cout << "Enter the radius of a circle: ";
    > cin >> radius;
    >
    > int area = Pi (radius * radius);


    Pi is no function.
    area should be float or double.

    > cout << endl
    > << "Area of the circle is = "
    > << radius


    Shouldn't that be area?

    > << endl;
    > return 0;
    > }
    >


    --
    Thomas
     
    Thomas J. Gritzan, Aug 13, 2006
    #5
  6. osmium posted:

    > That's an implicit multiply, computers aren't that intuitive; make it
    > explicit.
    >
    > int area = Pi*(radius*radius);



    They can be if we program them that way.

    float float::eek:perator()(float const i) { return *this * i; }

    We simply chose not to.

    --

    Frederick Gotham
     
    Frederick Gotham, Aug 13, 2006
    #6
  7. jdcrief

    osmium Guest

    "Frederick Gotham" wrote:

    > osmium posted:
    >
    >> That's an implicit multiply, computers aren't that intuitive; make it
    >> explicit.
    >>
    >> int area = Pi*(radius*radius);

    >
    >
    > They can be if we program them that way.
    >
    > float float::eek:perator()(float const i) { return *this * i; }
    >
    > We simply chose not to.


    I take it that you believe that response is helpful to the OP?
     
    osmium, Aug 13, 2006
    #7
  8. osmium posted:

    >> They can be if we program them that way.
    >>
    >> float float::eek:perator()(float const i) { return *this * i; }
    >>
    >> We simply chose not to.

    >
    > I take it that you believe that response is helpful to the OP?



    No, that would be an inaccurate observation.

    --

    Frederick Gotham
     
    Frederick Gotham, Aug 13, 2006
    #8
  9. jdcrief

    Daniel T. Guest

    In article <>,
    "jdcrief" <> wrote:

    > Complier: Visual C++ 2005 Express Edition
    >
    > The program I wrote will compile and execute, but the output is always
    > the same, no matter what number is entered in for the radius of the
    > circle. Someone please help! I am extremely new at this, as you can
    > probably tell. What am I missing with the code below?
    >
    >
    >
    > #include "stdafx.h"
    > #include <iostream>
    > #include <cmath>
    > using std::cout;
    > using std::cin;
    > using std::endl;
    >
    > int main() {
    > const float Pi = 3.14159;
    > radius = 0;


    'radius' was not declared. Try:
    int radius = 0;

    >
    > cout << "Enter the radius of a circle: ";
    > cin >> radius;
    >
    > int area = Pi (radius * radius);


    'Pi' can't be used as a function. Try:
    int area = Pi * radius * radius;

    > cout << endl
    > << "Area of the circle is = "
    > << radius


    The area of the circle is 'radius'? You declare the variable 'area' and
    assign it a value, but never use it...

    > << endl;
    > return 0;
    > }
     
    Daniel T., Aug 13, 2006
    #9
  10. jdcrief

    Kaz Kylheku Guest

    Frederick Gotham wrote:
    > osmium posted:
    >
    > > That's an implicit multiply, computers aren't that intuitive; make it
    > > explicit.
    > >
    > > int area = Pi*(radius*radius);

    >
    >
    > They can be if we program them that way.
    >
    > float float::eek:perator()(float const i) { return *this * i; }


    Thus if Pi names an object with this member function, it's possible to
    write:

    result = Pi(factor);

    Unfortunately, this abuses the meaning of the function calling
    parentheses, making them look like ordinary grouping parentheses, which
    is surprising when they are removed, as

    result = Pi factor;

    doesn't work. So this is actually silly, rather than intuitive.
     
    Kaz Kylheku, Aug 13, 2006
    #10
  11. Kaz Kylheku posted:

    > So this is actually silly, rather than intuitive.



    Indeed.

    The purpose of my post was to illustrate that computers don't have intuition
    nor intelligence, but rather they are programmed. The following:

    Pi(factor)

    doesn't work NOT because computers aren't intuitive enough, but rather
    because we chose to program them that way. The sample snippet was just to
    illustrate that we *could* have achieved such behaviour if we so desired, and
    that it would have nothing to do with any perceived "intuition" on the
    computer's part.

    --

    Frederick Gotham
     
    Frederick Gotham, Aug 13, 2006
    #11
  12. jdcrief

    Kai-Uwe Bux Guest

    Kaz Kylheku wrote:

    > Frederick Gotham wrote:
    >> osmium posted:
    >>
    >> > That's an implicit multiply, computers aren't that intuitive; make it
    >> > explicit.
    >> >
    >> > int area = Pi*(radius*radius);

    >>
    >>
    >> They can be if we program them that way.
    >>
    >> float float::eek:perator()(float const i) { return *this * i; }

    >
    > Thus if Pi names an object with this member function, it's possible to
    > write:
    >
    > result = Pi(factor);
    >
    > Unfortunately, this abuses the meaning of the function calling
    > parentheses, making them look like ordinary grouping parentheses, which
    > is surprising when they are removed, as
    >
    > result = Pi factor;
    >
    > doesn't work. So this is actually silly, rather than intuitive.


    Actually, it just demonstrates the need to have an overloadable whitespace
    operator :)


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Aug 13, 2006
    #12
  13. It occurred to me that Frederick Gotham wrote in comp.lang.c++:
    > Furthermore, you're using a signed integer type (and I presume your "area"
    > should never be negative).
    >
    > unsigned area = pi*radius*radius;


    There are plenty of reasons why one would want to use a default signed
    int, even if an unsigned int would work just fine, depending on what it will
    be used for. Mixed signed/unsigned operations can be hairy.

    The real question should be: If radius is a float, why make area an integer?

    --
    Martijn van Buul - - http://www.stack.nl/~martijnb/
    Geek code: G-- - Visit OuterSpace: mud.stack.nl 3333
    The most exciting phrase to hear in science, the one that heralds new
    discoveries, is not 'Eureka!' (I found it!) but 'That's funny ...' Isaac Asimov
     
    Martijn van Buul, Aug 13, 2006
    #13
  14. Martijn van Buul posted:

    >> Furthermore, you're using a signed integer type (and I presume your
    >> "area" should never be negative).
    >>
    >> unsigned area = pi*radius*radius;

    >
    > There are plenty of reasons why one would want to use a default signed
    > int, even if an unsigned int would work just fine, depending on what it
    > will be used for.



    Not "plenty", more like a handful.

    Mostly people use "int" out of laziness and ignorance; they want to store a
    number and they save themselves the psychological trauma of considering
    whether an unsigned type would be more appropriate.


    > Mixed signed/unsigned operations can be hairy.



    Nothing hairy -- it's quite simple. A quick tutorial for mixing integer
    signedness:

    (1) The first step is integer promotion.

    "signed char" and "signed short" becomes "signed int".

    "char", "unsigned char" and "unsigned short" become either "signed int"
    or "unsigned int" depending on the implementation.

    (2) Are they both the same size?

    If yes, they both become unsigned.

    If the smaller one is signed, they both become the larger unsigned type.

    If the larger one is signed, they both become the larger signed type if
    the larger signed type can hold all the values of the smaller unsigned type;
    otherwise they both become the unsigned variety of the larger type.

    Whether you choose to view this as "hairy", or whether you choose to learn it
    properly, will decide whether you're a good programmer, or an expert
    programmer.

    --

    Frederick Gotham
     
    Frederick Gotham, Aug 13, 2006
    #14
  15. jdcrief

    Jerry Coffin Guest

    In article <DYMDg.12631$>,
    says...
    > Martijn van Buul posted:


    [ ... ]

    > > There are plenty of reasons why one would want to use a default signed
    > > int, even if an unsigned int would work just fine, depending on what it
    > > will be used for.

    >
    > Not "plenty", more like a handful.


    A handful is plenty -- especially when there's almost _no_ good reason
    in the other direction.

    > Mostly people use "int" out of laziness and ignorance; they want to store a
    > number and they save themselves the psychological trauma of considering
    > whether an unsigned type would be more appropriate.


    You've tried to push this same basic idea before (e.g. in the thread at:
    http://tinyurl.com/ldj3b). Anybody who's interested should see that
    thread.

    > > Mixed signed/unsigned operations can be hairy.

    >
    > Nothing hairy -- it's quite simple. A quick tutorial for mixing integer
    > signedness:


    Here's a far quicker tutorial: avoid it! Doing so is almost always easy.

    [ ... ]

    > Whether you choose to view this as "hairy", or whether you choose to learn it
    > properly, will decide whether you're a good programmer, or an expert
    > programmer.


    I learned it long ago. That doesn't stop it from being hairy. I learned
    when to use it properly as well -- and that's almost never.

    Learning every quirk of the C++ standard doesn't make you an expert
    programmer. Code that requires such knowledge generally reflects vanity,
    not expertise. Writing code that only an expert can understand is easy.
    Coding a complex problem so even a beginner can understand it takes far
    more skill.



    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
     
    Jerry Coffin, Aug 14, 2006
    #15
  16. jdcrief

    Jim Langston Guest

    "Frederick Gotham" <> wrote in message
    news:DYMDg.12631$...
    > Martijn van Buul posted:
    >
    >>> Furthermore, you're using a signed integer type (and I presume your
    >>> "area" should never be negative).
    >>>
    >>> unsigned area = pi*radius*radius;

    >>
    >> There are plenty of reasons why one would want to use a default signed
    >> int, even if an unsigned int would work just fine, depending on what it
    >> will be used for.

    >
    > Not "plenty", more like a handful.
    >
    > Mostly people use "int" out of laziness and ignorance; they want to store
    > a
    > number and they save themselves the psychological trauma of considering
    > whether an unsigned type would be more appropriate.


    I use int when an unsigned int would work in most cases because it's
    generally easier to track bugs. If I have a value that's supposed to be
    positive, and it turns out to be negative, I know I have a bug. It the
    value just happens to be extremely large, I don't know if it's a valid value
    or a sign bit overflow.

    <snipped rest of reply>
     
    Jim Langston, Aug 14, 2006
    #16
  17. jdcrief

    Kaz Kylheku Guest

    Frederick Gotham wrote:
    > Those parentheses might mean multiplication in normal mathematics, but not
    > in C++. Furthermore, you're using a signed integer type (and I presume your
    > "area" should never be negative).
    >
    > unsigned area = pi*radius*radius;


    Riiiiight! Moreover, if the area is 255 or less, you should use
    unsigned char, and if it fits into the range 0 to 15, then this, of
    course:

    struct area {
    unsigned val : 4;
    };

    Let's exploit every low-level, machine-oriented representational quirk
    that we can.
     
    Kaz Kylheku, Aug 14, 2006
    #17
  18. Kaz Kylheku posted:

    > Frederick Gotham wrote:
    >> Those parentheses might mean multiplication in normal mathematics, but
    >> not in C++. Furthermore, you're using a signed integer type (and I
    >> presume your "area" should never be negative).
    >>
    >> unsigned area = pi*radius*radius;

    >
    > Riiiiight! Moreover, if the area is 255 or less, you should use
    > unsigned char



    If you're extremely scarce on memory, then yes. Otherwise, no.


    >, and if it fits into the range 0 to 15, then this, of
    > course:
    >
    > struct area {
    > unsigned val : 4;
    > };



    If you're extremely scarce on memory, then yes. Otherwise, no.


    > Let's exploit every low-level, machine-oriented representational quirk
    > that we can.



    Yes, let's go for "speed".

    --

    Frederick Gotham
     
    Frederick Gotham, Aug 14, 2006
    #18
  19. jdcrief

    Noah Roberts Guest

    Frederick Gotham wrote:
    > osmium posted:
    >
    > > That's an implicit multiply, computers aren't that intuitive; make it
    > > explicit.
    > >
    > > int area = Pi*(radius*radius);

    >
    >
    > They can be if we program them that way.
    >
    > float float::eek:perator()(float const i) { return *this * i; }



    1>c:\documents and settings\nroberts\my documents\visual studio
    2005\projects\playground\playground\playground.cpp(16) : error C2632:
    'float' followed by 'float' is illegal
    1>c:\documents and settings\nroberts\my documents\visual studio
    2005\projects\playground\playground\playground.cpp(16) : error C2039:
    '()' : is not a member of '`global namespace''
    1>c:\documents and settings\nroberts\my documents\visual studio
    2005\projects\playground\playground\playground.cpp(16) : error C2673:
    '()' : global functions do not have 'this' pointers

    What language are you programming in?
     
    Noah Roberts, Aug 14, 2006
    #19
  20. jdcrief

    kwikius Guest

    Frederick Gotham wrote:

    > Mostly people use "int" out of laziness and ignorance; they want to store a
    > number and they save themselves the psychological trauma of considering
    > whether an unsigned type would be more appropriate.


    hmm... so I could actually be psychologically damaged by even
    considering using an unsigned int? It follows that those that make
    liberal use of unsigned int when they dont need to, sooner or later
    turn raving mad from this practise.

    How long have you been following this practise ?

    regards
    Andy Little
     
    kwikius, Aug 14, 2006
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark
    Replies:
    1
    Views:
    604
    Larry Barowski
    Jun 27, 2005
  2. Replies:
    3
    Views:
    1,070
  3. hotadvice
    Replies:
    14
    Views:
    707
    hotadvice
    Oct 2, 2007
  4. oalfishcivil
    Replies:
    2
    Views:
    978
    Victor Bazarov
    Mar 24, 2009
  5. Andy B.
    Replies:
    0
    Views:
    809
    Andy B.
    Apr 30, 2010
Loading...

Share This Page