CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2011
    Posts
    153

    area and circumference of a circle

    Hi

    I have written a code but it's not working. It does compile. Please remember I'm a beginner. In the bold processing statements I have defined what "A" and "C" are. So if the area is greater or equal to 0, then display the value. Actually I don't want to use the processing statements again in if's cout. Please help me. Thanks a lot.

    Code:
    // program to calculate area of a circle
    
    #include <iostream>
    #include <cmath>
    #include <cstdlib>
    
    using namespace std;
    
    int main()
    
    {
        float R, A, C;
        cout << "Enter the radius R = ";
        cin >> R;
        A = 3.142*R*R;
        C = 2*3.142*R;
        if (R >= 0)
           {
           cout << "Area = A" << endl;
           cout << "Circumference = C" << endl;
            }
        else
            cout << "Invalid R" << endl;
        system ("PAUSE");
    }
    Last edited by heights; April 1st, 2011 at 10:19 PM.

  2. #2
    Join Date
    Aug 2009
    Posts
    440

    Re: area and circumference of a circle

    Code:
           cout << "Area = A" << endl;
           cout << "Circumference = C" << endl;
    These two lines don't do what you think. You need to actually output A and C...here you are merely outputting two strings (no numbers). The strings being "Area = A" and "Circumference = C" How would you normally output just a number to the screen?

  3. #3
    Join Date
    Mar 2011
    Posts
    153

    Re: area and circumference of a circle

    Quote Originally Posted by Alterah View Post
    Code:
           cout << "Area = A" << endl;
           cout << "Circumference = C" << endl;
    These two lines don't do what you think. You need to actually output A and C...here you are merely outputting two strings (no numbers). The strings being "Area = A" and "Circumference = C" How would you normally output just a number to the screen?

    Hi Alterah

    Thanks for pointing this out. I have corrected it.

    Code:
     if (R >= 0)
           {
           cout << "Area = " << A << endl;
           cout << "Circumference = " << C << endl;
            }

  4. #4
    Join Date
    Aug 2009
    Posts
    440

    Re: area and circumference of a circle

    The only other things I would point out (they won't effect what your code outputs) is to line up the closing brace on the if statement with the opening brace. And, also use braces for the else clause. I know they are technically required for one line statements, but it is good to get into the practice, and I think it makes the code more readable.

  5. #5
    Join Date
    Mar 2011
    Posts
    153

    Re: area and circumference of a circle

    Quote Originally Posted by Alterah View Post
    The only other things I would point out (they won't effect what your code outputs) is to line up the closing brace on the if statement with the opening brace. And, also use braces for the else clause. I know they are technically required for one line statements, but it is good to get into the practice, and I think it makes the code more readable.
    Thank you, Alterah. Yes, you are right, one has to be consistent.

    But sometimes my tab doesn't really align them well so I have space and backspace buttons manually. Suppose the character (let's say a brace located above the line where I'm at) I'm seeking alignment with, then pressing tab doesn't align it.

    As a result I have to check the column number of the character, and use space and backspace buttons for alignment purposes.

    Hope you could understand what I'm saying.

    Regards
    H

  6. #6
    Join Date
    Aug 2009
    Posts
    440

    Re: area and circumference of a circle

    That is probably an issue with Dev-C++. It truly is obselete. I would seriously consider updating to either Visual Studio C++ or Code::Blocks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured