Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > calculating the area of a circle problems

Reply
Thread Tools

calculating the area of a circle problems

 
 
jdcrief
Guest
Posts: n/a
 
      08-13-2006
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;
}

 
Reply With Quote
 
 
 
 
Kaz Kylheku
Guest
Posts: n/a
 
      08-13-2006
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.

 
Reply With Quote
 
 
 
 
osmium
Guest
Posts: n/a
 
      08-13-2006
"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;
> }
>



 
Reply With Quote
 
Frederick Gotham
Guest
Posts: n/a
 
      08-13-2006
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
 
Reply With Quote
 
Thomas J. Gritzan
Guest
Posts: n/a
 
      08-13-2006
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
 
Reply With Quote
 
Frederick Gotham
Guest
Posts: n/a
 
      08-13-2006
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:perator()(float const i) { return *this * i; }

We simply chose not to.

--

Frederick Gotham
 
Reply With Quote
 
osmium
Guest
Posts: n/a
 
      08-13-2006
"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: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?


 
Reply With Quote
 
Frederick Gotham
Guest
Posts: n/a
 
      08-13-2006
osmium posted:

>> They can be if we program them that way.
>>
>> float float: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
 
Reply With Quote
 
Daniel T.
Guest
Posts: n/a
 
      08-13-2006
In article <(E-Mail Removed) om>,
"jdcrief" <(E-Mail Removed)> 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;
> }

 
Reply With Quote
 
Kaz Kylheku
Guest
Posts: n/a
 
      08-13-2006
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: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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ code for calculating area of random shape oalfishcivil C++ 2 03-24-2009 12:21 PM
do structure definitions go in data area or in code area... hotadvice C Programming 14 10-02-2007 04:10 AM
Calculating area outside of an ARC marty.gagnon@imagenow.com Java 3 11-22-2005 07:24 PM
Add point to graphics area without having to paint the whole area? Mark Java 1 06-27-2005 03:09 AM
Jisatsu circle (Suicide Circle) Col's Cavern DVD Video 1 06-07-2004 06:55 PM



Advertisments