| | |
conversion type
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: May 2004
Posts: 11
Reputation:
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++
Also, keep in mind that variables can be typecasted. For example:
I think I did that right, anyways.
C++ Syntax (Toggle Plain Text)
int x = 5; float sum = (float)(x) + 2.25;
I think I did that right, anyways.
Dani the Computer Science Gal 
Follow my Twitter feed! twitter.com/DaniWeb
And if you're interested in Internet marketing there is twitter.com/DaniWebAds

Follow my Twitter feed! twitter.com/DaniWeb
And if you're interested in Internet marketing there is twitter.com/DaniWebAds
•
•
•
•
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.
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
Posts: 95
Reputation:
Solved Threads: 10
•
•
•
•
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
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: basic fstream stuff
- Next Thread: Cubing Digits Individually
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib library lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sorting spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






