4 Replies - 1330 Views - Last Post: 24 January 2010 - 10:04 PM Rate Topic: -----

#1 vplazed   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 28-September 09

Round Numbers

Posted 24 January 2010 - 03:52 PM

Hi, I am having some difficulty with rounding numbers. I not getting output of rounding. Also, I have some difficulty on rounding negative numbers. for example 3.4=3 3.5=4 -1.5=-1 -2.4=2. The output i am getting is 3.4...

#include <iostream.h>
#include <math.h>
int main()
{
double number;
int place;
cout<<"Enter a number"<<endl;
cin>>number;
	number *= 10^place;
	int istack = (int)floor(number);
	int out = number-istack;
if (out < 0.5){
floor(number);
number /= 10^place;
cout<<number<<endl;
return number;
}
if (out > 0.4) {
ceil(number);
number /= 10^place;
cout<<number;
return number;
}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Round Numbers

#2 seeP+   User is offline

  • D.I.C Addict

Reputation: 55
  • View blog
  • Posts: 601
  • Joined: 20-July 09

Re: Round Numbers

Posted 24 January 2010 - 04:04 PM

1st Your main function is an int but trying to return a double.
2nd You didn't include objects. For example a lot of people use 'using namespace std;'

This post has been edited by seeP+: 24 January 2010 - 04:06 PM

Was This Post Helpful? 0
  • +
  • -

#3 nmeans73   User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 59
  • Joined: 26-December 09

Re: Round Numbers

Posted 24 January 2010 - 09:00 PM

not sure if this changes anything but, unless you are using a very old compiler, your two includes at the top are incorrect. They should be:
#include <iostream>

//and

#include <cmath>


Was This Post Helpful? 0
  • +
  • -

#4 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1270
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: Round Numbers

Posted 24 January 2010 - 09:02 PM

Why are you trying to return values out of main??? :/

example round funciton
#include <iostream>
#include <cmath>

using namespace std; //<---neeed this

int round(double out)
{
	if (out<0)
		out-=0.5;
	else 
		out+=0.5;
	return (int)out;

}
int main()
{
	double number;
	cout<<"ENTER A NUMBER : ";
	cin>>number;

	cout<<round(number)<<endl;

	cin.ignore();
	cin.get();

	


	return 0;
}


This post has been edited by ImaSexy: 24 January 2010 - 09:07 PM

Was This Post Helpful? 0
  • +
  • -

#5 [email protected]   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 57
  • Joined: 06-February 08

Re: Round Numbers

Posted 24 January 2010 - 10:04 PM

int function doesnt return a duoble type
include 'using namespace std;' in begining
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1