|Please let me know if this could be better|
The main improvement would be to move the input code to a function, so you don't end up with 13 lines for each variable you get from the user. See cire's example for an example. And see (if you haven't already done so):
|So is this completely full-proof way to do this?|
To the best of my knowledge, yes.
But you could test it?
|or in which way could this simple operation be perfected even more?|
I think it is about as suscint as you can get.
But why not have a go trying to improve it? Or at least match it??
|Are the variables introduced done so in the best way?|
In C++ is is seen as good form to declare variables at the point of first use, or as close to it as possible. And in as tight a scope as possible. Declaring variables at the start of a function is old-school C (i.e. C89) style.
Some people (including me) also follow the rules:
- only a single declaration/definition per line
- initialize all variables
There are always exceptions to any rule (for example, I wouldn't usually inititialize a large buffer I was about to use to read file data), but uninitialized variables can cause real grief. So it is safer to make it the general rule that you initialize them, and only omit the initialization with good reason (with code comments to explain why.)
As you should be using declaration at first point of use most of the time anyway, you shouldn't have many variable to initialize before use. So the one-per-line and initialize-all rules aren't actually particularly arduous to comply with.
In your code this means line 40 should become
double z = x / y;
and line 12 deleted.
And lines 9 and 11 could be moved to just before
on line 27
And if you move the input code into a function, you could reduce your main down to something like
double x = getInput("Enter first number: ");
double y = getInput("Enter second number: ");
double z = (2 * x) / y;
cout << "New number = " << z << endl;