954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Problem With Flaoting Point Number?

i have a variable "float PI = 3.146;" in a header file called "evar.h" but when i use the variable PI in my main functoin it only comes out as "3" , i have to explictly type cast it for the number "3.146" to be printed out

how could i overcome this problem ?

RyanMcMillan
Newbie Poster
11 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

Well, you could post the code for starters.

gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
 
Well, you could post the code for starters.


well heres the header file

#ifndef _EVAR_H_
#define _EVAR_H_

float PI = 3.146;

#endif // _EVAR_H_

and the main code file

#include <cstdlib>
#include <iostream>
#include "evar.h"

using namespace std;

int main(int argc, char *argv[])
{
    extern float PI;
    
    cout << "PI Is Equal To: " << PI << endl;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

OUTPUT

PI Is Equal To: 3


this is just to try out external variables because i'm still learning

RyanMcMillan
Newbie Poster
11 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

Try moving line 9 to line 6.

gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
 
Try moving line 9 to line 6.


its ok now , i just omitted the extern decleration and it worked lol

RyanMcMillan
Newbie Poster
11 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

It shouldn't have.

Problem #1) Never define a variable in a header file. The statement float PI = 3.146; shouldonly be made in a source file. If you add the header to 4 source files, you will have 4 definitions of PI causing lots of errors.

Problem #2) The extern statement goes in the header file so other sources that include the .h file know that PI will be defined in another source.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

It shouldn't have.

Problem #1) Never define a variable in a header file. The statement float PI = 3.146; shouldonly be made in a source file. If you add the header to 4 source files, you will have 4 definitions of PI causing lots of errors.

Problem #2) The extern statement goes in the header file so other sources that include the .h file know that PI will be defined in another source.

well this is how my class thought me to do it, its called external variables they said ,

it is just like defining constants in a header file and the extern keyword tells the compiler that that variable is external and not to confuse with other "PI" variables from other namespaces ect...

RyanMcMillan
Newbie Poster
11 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

Try this:

cout << "PI Is Equal To: " << fixed << PI << endl;
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

well this is how my class thought me to do it, its called external variables they said ,

it is just like defining constants in a header file and the extern keyword tells the compiler that that variable is external and not to confuse with other "PI" variables from other namespaces ect...


I believe you misunderstood your class. And if not, you need an instructor that knows the language because your understanding is not correct.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 
I believe you misunderstood your class. And if not, you need an instructor that knows the language because your understanding is not correct.


ok then lol, be like that

RyanMcMillan
Newbie Poster
11 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: