Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Circle area

  1. Sep 4, 2005 #1

    JamesU

    User Avatar
    Gold Member

    What's wrong with this C++ project I'm working on? it's supposed to find the area of a circle:

    #include <cstdlib>
    #include <iostream>

    using namespace std;

    int main(int argc, char *argv[])
    {
    int pi = 3.14
    int rad
    int radsqr = rad*rad
    cout << "radius of the circle:" <<
    cin >> rad
    cout >> "The radius of the circle is about:" << radsqr*pi << end1;

    system("PAUSE");
    return EXIT_SUCCESS;
    }
     
  2. jcsd
  3. Sep 4, 2005 #2

    LeonhardEuler

    User Avatar
    Gold Member

    It's been a while since I've done programming, but looking at it quickly, I just notice that you said int pi =3.14, but int only stores integers so your really saying int pi =3 . This may not be the only problem, but its the one that I see.
     
  4. Sep 4, 2005 #3

    dduardo

    User Avatar
    Staff Emeritus

    Ahh what a mess!!! Try this:

    Code (Text):

    double pi = 3.14 ;
    double rad ;
    cout << "radius of the circle:" ;
    cin >> rad ;
    cout << "The radius of the circle is about:" << rad*rad*pi << endl ;
     
     
    Last edited: Sep 4, 2005
  5. Sep 4, 2005 #4

    JamesU

    User Avatar
    Gold Member

    oh, I meant 'the area of the circle is about:'

    but thanks.
     
  6. Sep 4, 2005 #5

    LeonhardEuler

    User Avatar
    Gold Member

    Oh, and you square rad before it gets the input from the user. That means that since rad starts out as random garbage before you give it a value, radsqr is random garbage squared. You have to square it after the line that says cin<<... to get what you want. So if you make that change and change the int's to float's it should work.
     
  7. Sep 4, 2005 #6

    JamesU

    User Avatar
    Gold Member

    grrr....Now I can't get the circumference to show up:
    Code (Text):

    #include <cstdlib>
    #include <iostream>

    using namespace std;

    int main(int argc, char *argv[])
    {

    double pi = 3.14 ;
    int rad ;
    cout << "radius of the circle:" ;
    cin >> rad ;
    cout << "The area of the circle is about:" << rad*rad*pi << endl;
    cout << "The circumference of the circle is about:" << rad*2*pi <<
        system("PAUSE");
        return EXIT_SUCCESS;
    }
     
    it only displays the area
     
  8. Sep 4, 2005 #7

    dduardo

    User Avatar
    Staff Emeritus

    Actually you probably want rad to be double too since you can have decimal values.

    Why do you have a hanging << at the end of the circumference line? Dont' forget the endl;
     
  9. Sep 4, 2005 #8

    JamesU

    User Avatar
    Gold Member

    it says:

    :grumpy:
     
  10. Sep 4, 2005 #9

    LeonhardEuler

    User Avatar
    Gold Member

    It's endl, not end1(end-lowercase L, not end-one)
     
  11. Sep 4, 2005 #10

    dduardo

    User Avatar
    Staff Emeritus

    Its the letter "L", but lower case. Not the number "1"
     
  12. Sep 4, 2005 #11

    JamesU

    User Avatar
    Gold Member

    oh, I'm stupid. It's the font on my compiler that's confusing me
     
  13. Sep 4, 2005 #12

    JamesU

    User Avatar
    Gold Member

    my formula calculation is complete:

    Radius of the circle: 3
    The area of the circle is about: 28.26
    The circumference of the circle is about: 18.84
    The diameter of the circle is: 6

    muahaha :devil:
     
  14. Sep 4, 2005 #13

    dduardo

    User Avatar
    Staff Emeritus

    Congrats! Now try something more difficult like calculate pascal's triangle.
     
  15. Sep 4, 2005 #14

    JamesU

    User Avatar
    Gold Member

    I'll work my way up.

    what was pascal's triangle? the 3/4/5 one? in trig?
     
  16. Sep 4, 2005 #15

    JamesU

    User Avatar
    Gold Member

    aha! basic trig :cool:

    Code (Text):

    #include <cstdlib>
    #include <iostream>

    using namespace std;

    int main(int argc, char *argv[])
    {
    double hyp
    double opp
    double adj
    cout << "Input sides of a right triangle
    cout << "Hypotenuse:" << endl;
    cin >> hyp
    cout << "opposite" << endl;
    cin >> opp
    cout << "adjacent" << endl;
    cin >> adj
    cout << "Triangle's properties are:" << endl;
    cout << "Sine:" adj/hyp " Cosine:" opp/hyp " Tangent:" opp/adj
    system("PAUSE");
    return EXIT_SUCCESS;
    }
     
     
  17. Sep 4, 2005 #16

    dduardo

    User Avatar
    Staff Emeritus

  18. Sep 4, 2005 #17

    JamesU

    User Avatar
    Gold Member

    unfortunately, I do not understand that :frown:

    The flaw in my trig program is that anyone can put the hypotenuse as a smaller length than the opposite or adjacent sides. could I do an if statement that if this happens, function main() will execute again?
     
  19. Sep 4, 2005 #18

    dduardo

    User Avatar
    Staff Emeritus

    Or you could just have a loop that continues to ask for the hypotenuse until it is an accepatable value.
     
  20. Sep 4, 2005 #19

    JamesU

    User Avatar
    Gold Member

    something like?

    for ((hyp<opp)||(hyp<adj))
    {
    cin >> hyp
    }
     
  21. Sep 4, 2005 #20

    dduardo

    User Avatar
    Staff Emeritus

    A do while loop would be appropriate:

    do {
    cin >> hyp ;
    } while ( (hyp < opp) || (hyp<adj) ) ;
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Circle area
  1. Circle algorithms (Replies: 1)

Loading...