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 [B]int[/B] type)
}

Im using the function in a switch statement and the other functions return float numbers.

I guess I could use local variables for that function, could I not?
sorry about that.

Also, keep in mind that variables can be typecasted. For example:

int x = 5;
float sum = (float)(x) + 2.25;

I think I did that right, anyways.

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 [b]int[/b] type)
}

The fmod function returns the remainder of floating point division.

something like this should work:

myfloat <- float
myint <- int

myint = 0;
while(myfloat >= 1){
myint++;
myfloat--;
}

Something like that would do the trick... I might have my reasoning a little of but I'm sure that it would work.

Hello guys,
is there a way to declare global variables as [B]float[/B] and then convert them to [B]int[/B] 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

Hello
you can use a most c++ powerful operator

int a;
float numq = 60.7;
a = static_cast<int> (num1);

i don't now why poeple here don't use a c++ feature the are using a c-style

...
i don't now why poeple here don't use a c++ feature the are using a c-style

I think people don't often realize there's more than one kind of cast in C++. If that's news to anyone, go look up casting in a good C++ reference and find out what they are and how they work.

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

two approaches that i can think of:

(1)

float num1, num2, result;
float Mod()
{
      float result;
      result = (float) ( (int) num1 % (int) num2 );
      return result;
}

(2)
use fmod()
check the use of it on mdsn.microsoft.com

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.