943,350 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4640
  • C++ RSS
May 29th, 2004
0

conversion type

Expand Post »
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)
}
Similar Threads
Reputation Points: 12
Solved Threads: 0
Newbie Poster
MaxC is offline Offline
11 posts
since May 2004
May 29th, 2004
0

Re: conversion type

Im using the function in a switch statement and the other functions return float numbers.
Reputation Points: 12
Solved Threads: 0
Newbie Poster
MaxC is offline Offline
11 posts
since May 2004
May 29th, 2004
0

Re: conversion type

I guess I could use local variables for that function, could I not?
sorry about that.
Reputation Points: 12
Solved Threads: 0
Newbie Poster
MaxC is offline Offline
11 posts
since May 2004
May 29th, 2004
0

Re: conversion type

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

C++ Syntax (Toggle Plain Text)
  1. int x = 5;
  2. float sum = (float)(x) + 2.25;

I think I did that right, anyways.
Administrator
Staff Writer
Reputation Points: 1422
Solved Threads: 162
The Queen of DaniWeb
cscgal is offline Offline
13,645 posts
since Feb 2002
May 29th, 2004
0

Re: conversion type

Quote 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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
May 29th, 2004
0

Re: conversion type

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.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
Natso is offline Offline
51 posts
since May 2004
May 30th, 2004
0

Re: conversion type

Quote 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
Reputation Points: 108
Solved Threads: 7
Posting Whiz in Training
FireNet is offline Offline
256 posts
since May 2004
Jun 7th, 2004
0

Re: conversion type

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
Reputation Points: 12
Solved Threads: 2
Newbie Poster
abu_sager is offline Offline
10 posts
since Jun 2004
Jun 7th, 2004
0

Re: conversion type

Quote 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
Reputation Points: 182
Solved Threads: 71
Posting Pro in Training
gusano79 is offline Offline
475 posts
since May 2004
Jun 9th, 2004
0

Re: conversion type

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ivosetyadi is offline Offline
1 posts
since Jun 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: basic fstream stuff
Next Thread in C++ Forum Timeline: Cubing Digits Individually





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC