You are here:

C++/Displaying results in metres and cm


I want to output the display answer of a conversion program to show meters, centimeters and millimeters in the display output. For example if the user enters in 1025mm to be converted to meters- the display output should show 1m 2cm 5mm. I have written the conversion program and it works, I just can't figure out how to break down the answer into parts.
Below I have entered the code for my program to make it easier for you to direct me. Thank you very much for your time!

#include <cstdlib>
#include <iostream>
#include <iomanip>
using namespace std;

int main(int argc, char *argv[])
   char length;
   double mili, centi, meter;
   cout<<"Enter a length in millimeters: ";
   cout<<"To convert to centimetres enter C, to convert to metres enter M."<<endl;
   if (length=='C')
       cout<<"Converts to "<<centi<<" centimetres."<<endl;
       cout<<"Converts to "<<meter<<" meters."<<endl;
   return EXIT_SUCCESS;

So this is not so much about C++ or any other programming language and more about the nature of integer (whole number) arithmetic.

Taking your example.

If you have 1025mm then dividing by 1000 (note _not_ 100 as you have it in your program), using integer (whole number) division gives 1 with a remainder of 25 (you might remember this from early school days).

One obvious way to obtain the remainder is to multiply the result of the division by the denominator and subtract this result from the numerator. So in the example we get:

   1025 - (1 * 1000) = 25

However you might like to know that we can obtain the remainder (or modulus) from an integer division in C++ (and C and some other languages) using the % operator.


   1025 / 1000 yields a result of 1
   1025 % 1000 yields a result of 25.

You then feed this remainder into the cm calculation:

   25 / 10 yields a result of 2
   25 % 10 yields a result of 5.

As this is almost certainly homework or coursework of some kind I shall leave it up to you to incorporate this information into your program.

It is always useful to be able to work out what you want without a computer first - then all you have to do is translate it into whatever programming language you are using. Know where you are going before you start driving!

Hope this helps and good luck with the assignment.  


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


©2016 All rights reserved.