User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 403,490 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,236 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 2986 | Replies: 9
Reply
Join Date: May 2004
Location: London UK
Posts: 11
Reputation: MaxC is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
MaxC MaxC is offline Offline
Newbie Poster

conversion type

  #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++
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2004
Location: London UK
Posts: 11
Reputation: MaxC is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
MaxC MaxC is offline Offline
Newbie Poster

Re: conversion type

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

Re: conversion type

  #3  
May 29th, 2004
I guess I could use local variables for that function, could I not?
sorry about that.
MSVC++
Reply With Quote  
Join Date: Feb 2002
Location: Lawn Guylen, NY
Posts: 10,893
Reputation: cscgal is just really nice cscgal is just really nice cscgal is just really nice cscgal is just really nice cscgal is just really nice 
Rep Power: 32
Solved Threads: 111
Admin
Staff Writer
cscgal's Avatar
cscgal cscgal is online now Online
The Queen of DaniWeb

Re: conversion type

  #4  
May 29th, 2004
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.
Reply With Quote  
Join Date: Apr 2004
Posts: 3,477
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 16
Solved Threads: 141
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: conversion type

  #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  
Join Date: May 2004
Posts: 53
Reputation: Natso is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 1
Natso Natso is offline Offline
Junior Poster in Training

Re: conversion type

  #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  
Join Date: May 2004
Posts: 250
Reputation: FireNet will become famous soon enough FireNet will become famous soon enough 
Rep Power: 6
Solved Threads: 6
FireNet's Avatar
FireNet FireNet is offline Offline
Posting Whiz in Training

Re: conversion type

  #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  
Join Date: Jun 2004
Posts: 10
Reputation: abu_sager is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 2
abu_sager abu_sager is offline Offline
Newbie Poster

Re: conversion type

  #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  
Join Date: May 2004
Location: Portland, OR, USA
Posts: 48
Reputation: gusano79 is on a distinguished road 
Rep Power: 5
Solved Threads: 0
gusano79 gusano79 is offline Offline
Light Poster

Re: conversion type

  #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  
Join Date: Jun 2004
Location: Jakarta
Posts: 1
Reputation: ivosetyadi is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
ivosetyadi's Avatar
ivosetyadi ivosetyadi is offline Offline
Newbie Poster

Re: conversion type

  #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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the C++ Forum

All times are GMT -4. The time now is 12:17 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC