(C++) How would you go about rounding numbers?

Ok Basically when I am run the program it will add both numbers 1.67 + 1.32 and give the result 3.00. Clearly, the sum of the displayed numbers should be 2.99 and not 3.00. The problem is that although the values in a and b have been displayed with two decimal digits, they were added internal to the program as three-digit numbers. The solution is to round the values in a and b before they are added to the statement c = a + b;

Using the int cast, how would I be able to round the values in variables a and b to the nearest hundredth(penny value) before they are added?

Code:

`#include <iostream.h>`

#include <iomanip.h>

void main(void)

{

float a, b, c;

a = 1.674;

b = 1.322;

cout << setprecision(3) << a << endl;

cout << setprecision(3) << b << endl;

cout << "----\n";

c = a + b;

cout << setiosflags(ios::showpoint)

<< setprecision(3) << c << endl;

}

Re: (C++) How would you go about rounding numbers?

Quote:

*Originally posted by jeffcoulter *

**The problem is that although the values in a and b have been displayed with two decimal digits, they were added internal to the program as three-digit numbers.**

Forget that.

There's nothing strange with you program above. 3.00 is the correctly rounded value to 2.998