You are here:

C++/doing division


i am trying to do a division program my daughter can use to learn to do division. i can get everything working except for anything with a remainder. i used the modulous operator to take care of the remainder but i would prefer to have the correct answer to 2 decimal places. for example: 5 / 3 = 1.666666666 etc., i would like to have the answer come out at 1.67. the way i have it now if the user put 1.67 the program says the answer is incorrect, but if the user puts in 1 it is correct. hopefully you can help me with this problem. this is not for school, i have already taken a c++ course about a year ago and am now trying to learn more on my own.

This is not so much a C++ question as far as I can tell, but a general point about the different numeric types used by computers.

From what you say you are doing something like:

       int numerator(0);
       int denominator(1);

       std::cout << "Enter a numerator: ";
       std::cin >> numerator;

       std::cout << Enter a denominator: ";
       std::cin >> denominator;

       std::cout << "Numerator divided by denominator = "
         << numerator / denominator << '\n';

This is quick and dirty omitting all error checking and more robust input techniques, but it gets the idea across.

My _big_ assumption - which I inferred from your question as you did not state it explicitly - is that you are using integer types to do the calculation. Integers have no fractional part. They are whole numbers. Hence if you get a fractional part when doing a calculation it is lost (technically the result is truncated so for example results of 1.1 and 1.9999 both become 1).

If you wish to use real numbers then the type to use is called a floating point type. C and C++ have two main floating point types float and double. The double type is typically twice the size of a float and stores a wider range of numbers with greater precision. There is a third type - long double - which gives the possibility of even greater range and precision (it may just be the same size as a double if the machine cannot support such a type). You will notice that I used terms such as range an precision - as with all computer types floating point types are not infinite so there are limits to the values they can store. The big idea is that they can store very large or small numbers with the same precision within that scale.

In your case I would think that a float would suffice - change your integer values to float (or double) values:

       float numerator(0.0);
       float denominator(1.0);

Finally, if you are using C++ IOStreams to output the results then you have some control over the format used to output the value. You can specify that a decimal point be displayed and whether the number is displayed using fixed point (123.45) or scientific notation (1.2345e2), the default is to let the system decide based on the value. You can also specify the precision of the value, which generally means the number of decimal places displayed - values will be rounded rather than truncated - so 12.3456 with a precision of 2 and fixed point format is shown as 12.35. To use the format manipulators include the C++ standard header <iomanip>.

Here is an example:

       std::cout << std::fixed << std::setprecicision(2)
         << result << std::endl;

Which specifies that you wish to use fixed point format, and that 2 significant digits should be displayed.

Note: I have not tested any of the code snippets shown here so I apologise for any and all typos and mistakes.

I would further suggest you obtain some good C++ reference material.

Good luck and I hope you and your daughter learn much and (importantly) have fun doing so.  


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.


My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at


©2017 All rights reserved.