problem with floor and round

Hi all.I have some problems with round, floor, ceil and so on.I want my program to output only integers, and it doesnt work with round ceil and so on and i dont know why.
Here is an example of a code where floor and round and ceil cant do what i want to.(or i make it wrong).(mine is too big i just made a sample for here for you to see what the problem is)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <stdlib.h>
#include <cmath>
using namespace std;
double x,x1;
double func(double test){
 test=x+x1;
return test;
}
int main()
{
cout<<"please enter a number";
  cin>> x;
  cout<<"please enter a number";
  cin>>x1;
  
  
  if (round(func)==func)cout<<func<<"\n";
  system("PAUSE");	
  return 0;
}


First of all it doesnt compile.And if I somehow make it to compile it will output the result watever it is (int or double) or it will output no result.
What should i do, how to sort the integers from the floating point nums?
Any ideas?

Last edited on
In
 
 if (round(func)==func)cout<<func<<"\n";


you don't call func, you round the function pointer of func.

Also, please don't write it like that. It looks ugly if you put the command in the same line with the if statement X_X

Also, a number of things that you might wanna avoid in future programs (I know this is just a small test thingy, so no big deal but anyways):
Unnecessary use of global variables.
No error checking after input.
system("Pause")- no big deal in test programs, but don't let it become a habit.
AND the worst of them all: you pass a completely useless parameter to func.

To sort out integers/floating point values you might want to let the user input a string, and then check wether it's an integer or a floating point number.
Last edited on
There are numerous errors I can count with your program. The main being around line 18.

Your attempting to send a function (not a function's return value, but the function pointer) to round, which is what the error is. Also your initializations are a little odd to me. Why did you put them in the global scope?

If you want func() to return the addition of x and x1 then it should look like this:
1
2
3
4
double func(double x, double x1)
{
   return x + x1;
}

then your calls at line 18 should look like this:
func(x, x1);

Also move the variables x and x1 out of the global scope. You should never have to define a variable as global unless its EXTREMELY important to do so. (When variables are global it can cause more problems when you are coding in the future than it can solve.)

Just move the variable down here:
1
2
3
4
5
int main()
{
   double x, x1;
   //more code
}


Then to cout only the integers and not any trailing zeros simply cast your value to an int with static_cast or C style castings.
Dont pay so much attention to this code i said its just an example.(Nevertheless I will have in mind some of your advises!)
Wolfgang you said something about casting.I dont get the idea.Can you show an example.
How will you change that using cast? :
1
2
  if (round(func)==func)cout<<func<<"\n";
Simple, you don't need to round there, you could also cast the result to int with
 
(int)func();


or
 
static_cast<int>(func());


- that would do the same thing as floor. But I'd personally not suggest doing that. But as long as you're aware you didn't actually call the function...
Last edited on
so Hanst99 if i change
if (round(func)==func)cout<<func<<"\n";
with
(int)func();
or
static_cast<int>(func());
it will check if the output is an int ?and after that how should i call the function to output the integer(if its an integer)?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <cstdlib>
#include <cmath>

using namespace std;
// No Global Variables!

double func(double x, double x1)
{
   return x + x1;
}

int main()
{
  double x, x1; // Local Variables! :D
  cout<<"please enter a number";
  cin>> x;
  cout<<"please enter a number";
  cin>>x1;

  // Note how I call the functions: "func(x, x1)" NOT "func"
  double roundedNum = round( func(x, x1) ); // Store the return of this to a variable
  double funcVal = func(x, x1); // Store return to another variable.
  if (roundedNum == funcVal)
     cout << funcVal << "\n";


  system("PAUSE"); // Don't get use to using this!
  return 0;
}


How your code should sort of look like. You don't need all the variables I made (you only really need x and x1) I just made more to show you how the calls to func() should look. Casting is another beast. Don't worry about it. If you get 12.000000000000000 printed out then come back, otherwise don't worry about casting right now.
It works perfectly.Thank you a lot for the help :)
Topic archived. No new replies allowed.