I'm working on some code for a school project that involves outputting the average GPA of a group of male and female students. The total GPA (before dividing to get the average) needs to be rounded as each student's GPA is added in order to get the correct output. However, the only way I know to round a double is to use the formatting command setprecision, and that only works if you're outputting the data, and I won't be outputting the data until the average is calculated. (the input data is coming from an input file, the variable for which is "in.")

Here's the relevant section of code:

//while not at end of input file, continue adding data to GPA totals.
while (!in.eof())
		in >> gender;     //input data from file
		in >> student;
		switch (gender) {     //switch to code for male or female
			case 'f':
			case 'F':
				ftotal += student;     //add student's GPA to total
				fctr++;     //add 1 to counter 

			case 'm':
			case 'M':
				mtotal += student;     //add student's GPA to total
				mctr++;     //add 1 to counter

				cout << "Input File Error.  Please check input." << endl;

I need some sort of command to round mtotal and ftotal to 2 decimal places before the loop restarts each time. Any help would be greatly appreciated.

Recommended Answers

All 9 Replies

Good news.. the solution to your problem is wicked simple.

May I recommend the ceil() and floor() functions of the <cmath> library.

Or you can use the setprecision() function of the iomanip lib.

#include <iomanip>
cout<<//output data//


Thanks a lot, but I don't think the ciel() and floor() functions are quite what I'm looking for. Will they round to two decimal places? And is there a way to make .5-.9 round up while .1-.3 rounds down? (Obviously, I'm not familiar with them.)


I'd use setprecision(2) that way, but I have to store the rounded values back into mtotal and ftotal for the next run through the loop.

or you can just make your own function :

int round(float num){	
	return (int)(num+0.5);
int roundDown(float num){
	return (int)(num);

example output :

Enter a number : 3.14
Round = 3
roundDown = 3

Enter a number : 3.5
Round = 4
roundDown = 3

For negative values you will have to do a little more, but I don't think
GPA should be negative, so there is no need.

Are you trying to round it off to 2 decimals place to be used in calculations for GPA? or something of the sort. I dont see how have extra decimal places used in calculations would be bad it would make your math solution more accurate THEN round the output to 2 decimal places. Hope you understand what Im saying

oh, didn't see you need to round off to 2 decimal places....

setpricision is the way to go then.


I know exactly what you're saying. And I agree. The problem is, it's for a school project, and my professor gave us what he wants the output to come out as - and apparently he did it the way I described, instead of the more accurate way. It's ridiculous, I know - to do all the calculations first would be easier and more accurate, but... that's not what he did.


I like the idea. Thanks. If I can figure out a way to get the program to choose whether to use the rounded up or rounded down number, that will work. (maybe I can read in the thousandths place and do it that way...)


I just went to try that out and realized I can't use it - it only works if you're rounding to whole numbers, and I need two decimal places. Thanks a lot though. It may very well turn out to be part of my solution.

oh, didn't see you need to round off to 2 decimal places....

setpricision is the way to go then.

No, not really, if the OP wants the value to be rounded to 2 decimal
places then he needs to convert that value into a two decimal
placed value, setprecision just output(i.e makes it appear) the final
value as rounded to 2 decimal place.

Maybe this will help;
//Its not perfectly precise

float myConvert(float num){
	return  ( int(num*100)/100.0f );


It had actually just occurred to me to multiply by 100, convert to int, and then divide by 100 to get my two decimal places back. Then I looked on here and saw your post - lol. I think I can make that work. Thanks a lot for the help!

I'm discussing the rounding thing (how it doesn't make much sense to do so) with my professor right now, so it may end up being a non-issue after all. But either way, it was a good learning experience. Thanks again to everyone!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.