| | |
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.
•
•
•
•
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: 80
Reputation:
Solved Threads: 5
•
•
•
•
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 |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets







