conversion type

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2004
Posts: 11
Reputation: MaxC is an unknown quantity at this point 
Solved Threads: 0
MaxC MaxC is offline Offline
Newbie Poster

conversion type

 
0
  #1
May 29th, 2004
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)
}
MSVC++
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 11
Reputation: MaxC is an unknown quantity at this point 
Solved Threads: 0
MaxC MaxC is offline Offline
Newbie Poster

Re: conversion type

 
0
  #2
May 29th, 2004
Im using the function in a switch statement and the other functions return float numbers.
MSVC++
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 11
Reputation: MaxC is an unknown quantity at this point 
Solved Threads: 0
MaxC MaxC is offline Offline
Newbie Poster

Re: conversion type

 
0
  #3
May 29th, 2004
I guess I could use local variables for that function, could I not?
sorry about that.
MSVC++
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 12,056
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 129
Administrator
Staff Writer
cscgal's Avatar
cscgal cscgal is offline Offline
The Queen of DaniWeb

Re: conversion type

 
0
  #4
May 29th, 2004
Also, keep in mind that variables can be typecasted. For example:

  1. int x = 5;
  2. 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
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,452
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 251
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: conversion type

 
0
  #5
May 29th, 2004
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)
}
The fmod function returns the remainder of floating point division.
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 53
Reputation: Natso is an unknown quantity at this point 
Solved Threads: 1
Natso Natso is offline Offline
Junior Poster in Training

Re: conversion type

 
0
  #6
May 29th, 2004
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.
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 256
Reputation: FireNet will become famous soon enough FireNet will become famous soon enough 
Solved Threads: 6
FireNet's Avatar
FireNet FireNet is offline Offline
Posting Whiz in Training

Re: conversion type

 
0
  #7
May 30th, 2004
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
See what you can, remember what you need

Fourzon | Earn via Coding
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 10
Reputation: abu_sager is an unknown quantity at this point 
Solved Threads: 2
abu_sager abu_sager is offline Offline
Newbie Poster

Re: conversion type

 
0
  #8
Jun 7th, 2004
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
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 95
Reputation: gusano79 is on a distinguished road 
Solved Threads: 10
gusano79 gusano79 is offline Offline
Junior Poster in Training

Re: conversion type

 
0
  #9
Jun 7th, 2004
Originally Posted by abu_sager
...
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 1
Reputation: ivosetyadi is an unknown quantity at this point 
Solved Threads: 0
ivosetyadi's Avatar
ivosetyadi ivosetyadi is offline Offline
Newbie Poster

Re: conversion type

 
0
  #10
Jun 9th, 2004
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC