-9

This round() is not under math.h header. How to make it work?

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float i=5.4;
printf("%f\t%f",i,round(i));
getch();
}
Magisch
  • 7,312
  • 9
  • 36
  • 52
dpraajz
  • 1
  • 2

2 Answers2

2

It's not available. You need to write you own using floor and ceil.

But better yet, get an up-to-date compiler. There's no excuse to be still using Turbo C++, and the language and libraries have changed incredibly since 1993.

Roddy
  • 66,617
  • 42
  • 165
  • 277
  • Yes, it is true, but keep in mind many students are still pushed to use Turbo C++ for learning at University !!! – ares777 Jan 12 '16 at 10:46
  • 7
    Then they should switch universities. After all, if the purpose of the university is to teach young skulls full of mush the necessary skills for them to find gainful employment, any university like this one would be a major fail. – Sam Varshavchik Jan 12 '16 at 12:03
  • 1
    Agree with Sam; I wouldn't hire a candidate who wrote code like that like that during the interview. So your university is teaching you things that will actively cause you to NOT be hired. – MSalters Jan 12 '16 at 13:46
  • Which compiler is best to use? @Roddy – dpraajz Jan 12 '16 at 18:44
  • @DeeprajBhujel "Best" is impossible to say, but if I was learning I'd want a free, commonly-used and modern compiler that could run on many platforms. Like *gcc* for instance. https://en.wikipedia.org/wiki/GNU_Compiler_Collection – Roddy Jan 12 '16 at 20:37
2
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float f=5.4;
int rounded,k;
k=f//Initialising the value of k as the integral value of f
if((f-k)>=0.5)
{
 rounded = k+1;
}
else
{
 rounded = k;
}
printf("The rounded value is %d",rounded);
getch();
}
  • I think this code runs fine in Turbo-C++ and will give you your desired answer. – gautamkvashisht Jan 14 '16 at 15:31
  • Also, Turbo-C++ may be old but it is very effective and using various logics, you can mostly do all what you need. – gautamkvashisht Jan 14 '16 at 15:33
  • No its not. The code you wrote is not paradigmatic C++ and will be difficult to maintain in the future. For example conio.h isn't found on modern systems. There is a reasonable chance that your rounding function doesn't have the correct behavior for all floating point values (such as nans). – Mikhail Aug 26 '16 at 02:46