•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 403,490 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,236 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 2986 | Replies: 9
![]() |
•
•
Join Date: May 2004
Location: London UK
Posts: 11
Reputation:
Rep Power: 5
Solved Threads: 0
Hello guys,
is there a way to declare global variables as float and then convert them to int to be used in a function? i.e.
is there a way to declare global variables as float and then convert them to int to be used in a function? i.e.
float num1, num2, result;
float Mod(){
num1%num2=result; //Wrong, (%) requires int type)
}
MSVC++
•
•
Join Date: Feb 2002
Location: Lawn Guylen, NY
Posts: 10,893
Reputation:
Rep Power: 32
Solved Threads: 111
Also, keep in mind that variables can be typecasted. For example:
I think I did that right, anyways.
int x = 5; float sum = (float)(x) + 2.25;
I think I did that right, anyways.
Dani the Computer Science Gal
Do you run a computer-related website? Feature it in our niche link directory!
Do you run a computer-related website? Feature it in our niche link directory!
•
•
•
•
Originally Posted by MaxC
Hello guys,
is there a way to declare global variables as float and then convert them to int to be used in a function? i.e.
float num1, num2, result; float Mod(){ num1%num2=result; //Wrong, (%) requires int type) }
•
•
•
•
Originally Posted by MaxC
Hello guys,
is there a way to declare global variables as float and then convert them to int to be used in a function? i.e.
yea,sure thing use type casting.
Eg:
float x = 9.879;
int y = (int)x; //y will be 9
you can do it the other way around too.
x=(float)y;
Usually float and int can be assigned to each other as C++ normally does that type casting automatically
•
•
Join Date: May 2004
Location: Portland, OR, USA
Posts: 48
Reputation:
Rep Power: 5
Solved Threads: 0
•
•
•
•
Originally Posted by abu_sager
...
i don't now why poeple here don't use a c++ feature the are using a c-style
Here's a quick article on why to use C++ casting:
http://www.sjbrown.co.uk/static_cast.html
For this problem, though, a C-style cast isn't very dangerous since we're only dealing with numeric types--the really bad stuff happens when you're casting pointers to classes.
--sg
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Other Threads in the C++ Forum
- Previous Thread: basic fstream stuff
- Next Thread: Cubing Digits Individually



Linear Mode