Using setprecision w/ cprintf

Hi! I'm a c++ student and I'm using Borland 5.02 as my compiler. How can I use setprecision with cprintf? I want to set the number of digits after the decimal point to two. I know how to do it with 'cout' but I don't know how to do it with 'cprintf'. The program kinda looks like the one below...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>

void main(void)
   {
   float a,b,sum=0;
   clrscr();

   textcolor(WHITE); textbackground(GREEN);
   cout<<"\nEnter first digit: ";
   cin>>a;
   cout<<"\nEnter second digit: ";
   cin>>b;

   sum=a+b;

   cprintf("\nThe sum is %f",sum);
   getch();
   }


The output (w/o setprecision) sets the number of digits after the decimal point to six.
1
2
3
int prec;
float value;
cprintf( "A number: %.*f", prec, value );

Good luck!
Hey, thanks a lot!!! It worked!!!
I should have mentioned that you can also directly encode the precision:

cprintf( "A number: %.2f", value );

:-)
Last edited on
Oh it's okay. You've been a great help! The first one already worked and I'm fine with that. Thanks again...
Topic archived. No new replies allowed.