LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-12-2005, 05:08 PM   #1
Beads
Member
 
Registered: Nov 2004
Location: Washington
Distribution: Mint
Posts: 104

Rep: Reputation: 17
Angry Odd math result


Simple routine to calculate the area of a circle:

#include <iostream>
useing namespace std;

int main() {
float radius;
float area(3.14159 * radius * radius);'

cout << "Enter the radius ";
cin >> radius;
cout << "Area is: " << area << endl;
}


Now, a radius of 5 should yield a result of 78.xxxx (irr). But this routine yields 12.xxx.
Anyone have any ideas as to why this routine doesn't work?

 
Old 02-12-2005, 05:30 PM   #2
csfalcon
Member
 
Registered: Jun 2004
Location: MD
Distribution: Fedora Core
Posts: 269

Rep: Reputation: 31
is this your complete code? next time please copy and paste to avoid typo.

you need to do the calculation of area after you get the radius with cin.

Code:
#include <iostream>
using namespace std;

int main() {
   float radius;
   float area;

   cout << "Enter the radius ";
   cin >> radius;
   area = 3.14159 * radius * radius;
   cout << "Area is: " << area << endl;
}
 
Old 02-12-2005, 07:42 PM   #3
Beads
Member
 
Registered: Nov 2004
Location: Washington
Distribution: Mint
Posts: 104

Original Poster
Rep: Reputation: 17
You are quite correct, csfalcon. That worked.
Thank you very much!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
math.h Damicles Programming 9 12-04-2010 05:51 AM
df gets different result from du? liyuefu Linux - General 4 10-01-2005 10:45 AM
Math Program evian Linux - Software 4 09-09-2004 10:41 AM
"ifconfig -a" giving odd result balajipriya Linux - Networking 0 08-11-2004 02:39 PM
math.h is empty ?? niac Programming 1 11-12-2002 01:10 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration