mile to kilo program

Hello everyone so I wrote this simple console program to convert user input miles to kilo (assignment in a book). I know it's a really basic piece of code but I'd to see if the code be be better improved. All constructive criticism will be appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  #include <iostream>

using namespace std;


int main()
{
	int input;
	const double kilo = 1.609;
	
	while (cin >> input)
	{
		double convert = input * kilo;
		cout << "You entered " << input << " miles and that equals to: " << convert << " kilo" << endl;
	}

	return 0;
}
Last edited on
From user's point of view:
"Kilo is a decimal unit prefix in the metric system denoting multiplication by one thousand."
https://en.wikipedia.org/wiki/Kilo-
(My first assumption on seeing "kilo" was a unit of mass: kilogram. Converting distance to mass did not sound trivial.)


Is "kilo" the most expressive name for the constant ratio of miles and kilometres? Good names are hard, really hard to come up with.


The std::endl does two things: 1) add a newline character to stream and 2) flush the stream. It is usually sufficient to write just the newline character '\n'.


The generic using namespace std; directive can introduce subtle problems in more complex programs.
In https://msdn.microsoft.com/en-us/library/5cb46ksf.aspx
The using directive allows all the names in a namespace to be used without the namespace-name as an explicit qualifier. Use a using directive in an implementation file (i.e. *.cpp) if you are using several different identifiers in a namespace; if you are just using one or two identifiers, then consider a using declaration to only bring those identifiers into scope and not all the identifiers in the namespace. If a local variable has the same name as a namespace variable, the namespace variable is hidden. It is an error to have a namespace variable with the same name as a global variable.


Example of the using declaration:
1
2
3
4
5
6
7
8
#include <iostream>

int main() {
  using std::cout;
  cout << "Hello ";
  cout << "world\n";
  return 0;
}


Keep the using directives and declarations (if any) in the .cpp files. Never in headers.
Topic archived. No new replies allowed.