calculating the area of a circle problems

J

jdcrief

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;
}
 
K

Kaz Kylheku

jdcrief said:
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.
 
O

osmium

jdcrief said:
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.
 
F

Frederick Gotham

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;
 
T

Thomas J. Gritzan

jdcrief said:
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?
 
F

Frederick Gotham

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.
 
O

osmium

Frederick Gotham said:
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?
 
D

Daniel T.

"jdcrief said:
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...
 
K

Kaz Kylheku

Frederick said:
osmium posted:



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.
 
F

Frederick Gotham

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.
 
K

Kai-Uwe Bux

Kaz said:
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
 
M

Martijn van Buul

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?
 
F

Frederick Gotham

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.

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.
 
J

Jerry Coffin

Martijn van Buul posted:

[ ... ]
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.
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.
 
J

Jim Langston

Frederick Gotham said:
Martijn van Buul posted:


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>
 
K

Kaz Kylheku

Frederick said:
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.
 
F

Frederick Gotham

Kaz Kylheku posted:
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".
 
N

Noah Roberts

Frederick said:
osmium posted:



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?
 
K

kwikius

Frederick said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top