| | |
conversion type
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
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: 9
•
•
•
•
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
Views: 4090 | Replies: 9
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment based beginner binary c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multidimensional multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






