#include <iostream>
usingnamespace std;
int main()
{
int a;
int b;
int c;
int d;
int e;
cout << "\t Welcome, I will try to guess the minimum and maximum" << endl;
cout << "\t numbers of your chosen numbers." << endl;
cout << "Please enter 5 numbers: " << endl;
cin >> a;
cin >> b;
cin >> c;
cin >> d;
cin >> e;
cout << "Your chosen numbers: " << a << b << c << d << e;
}
Please bear in mind that I haven't finished and I still need to add some more variables, I like to get some variables I know I need and work through the program, then I will know which one's to add as I progress.
Now, when I build and run this. The user enters 5 numbers, but the program prints them out as one big number - how can I prevent this.
I'm also looking for a more efficient way to declare and initialize the 5 numbers.
Problem one "...but the program prints them out as one big number..."
The thing is the << operator doesn't put spaces between what is being printed.
So print a space explicitly in between like "<< ' ' <<"
Another way of doing it is use the "setw" IO manipulator like cout << "Your chosen numbers: " << setw(6) << a << b << c << d << e;
Of course use number of your choice instead of 6.
Problem two "I'm also looking for a more efficient way to declare and initialize the 5 numbers."
I dont think there is anything inefficient, program execution wise, it the way you have declared the variables. If you mean efficient for typing then you can do int a=0, b=0, c=0, d=0, e=0; OR
[Note to OP: codewalker's and Endl3ss' declarations are not forms of efficient initialisation; there's no such thing as an "efficient declaration". codewalker's second solution isn't even initialisation. Also, multiple declarations on a single line is best avoided. --end note]
@Framework: There is no such thing as efficient declaration. I also agree that second solution is not initialisation, but the second line in the second solution is *logical initialisation* of the variables wont you agree?.
And of course first solution is more efficient in compiler point of view.
In general int a = 0; IS better than int a; a = 0;
@DJLad15: "neat looking" is subject to coding style. I agree with Framework that multiple declarations on single line is in general bad style;
Also, declaring multiple variables for multiple inputs works fine for very small number of inputs. If you need more than two inputs of the same type make it an array or vector, as a thumb rule.
"but the second line in the second solution is *logical initialisation* of the variables wont you agree?"
No, I don't agree, because it's not initialisation. Why not initialise them all to zero during a declaration context?
codewalker wrote:
"And of course first solution is more efficient in compiler point of view."
That may be the case, but multiple declarations on a single line is not the best of ideas. Since all of the variables have the same purpose, it's best to group them into an array just like I did in my example.
Well as I said int a =0; is better so it should be done that way. But by logical initialisation I mean "logically every variable should have initial value before first usage" and the second statement from second solution does that, nothing more