Rounding in C++

+1 Lee Garcia · November 8, 2015
I am having to write a math tutorial program for a lab. To contain all 4 basic math functions, add, subtract, multiply, & divide. Using the rand num generator to supply to numbers to user and a CIN for them to answer.

I've been able to set them all up with relative ease with the exception of the division tutor. Problem I'm having is rand num generator is populating numbers that when divided and the answer has infinite decimal values the user has no way of answering correct. 

For example 10/3 is 3.333333

I know how to use set precision for output values but how do round the assigned value of a variable to the nearest tenth or hundredth decimal place? The instructions say the program should be written for 4th to 6th graders and I know the division tutor should allow the users to answer to at least 1 remainder. 

Any ideas on how to accomplish this?

Post a Reply


Oldest  Newest  Rating
+1 Jon Z. · November 8, 2015
try giving an error threshold. let's say if you want precision of 2 decimals try something like
if(abs(exactAnswer-answerGiven) < 0.01){ //right answer! }
0 Nelson wong · November 9, 2015
just put 0.0 at your final variable when you calculate. 
  • 1



Used in many types of software including music players, video games, and many large scale applications.

Bucky Roberts Administrator