Help with simple program

Here's the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>

using namespace 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.
closed account (zb0S216C)
The solution is a "well, duh!" one:

 
std::cout << "Your chosen numbers: " << a << " " << b << " " << ...;

DJLad15 wrote:
"I'm also looking for a more efficient way to declare and initialize the 5 numbers."

Declare each variable when it's needed:

1
2
int a(0); // Initialise; same as: int a = 0;
std::cin >> a;

Wazzak
Last edited on
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
1
2
3
int a, b, c, d, e;

a = b = c = d = e =0;
or for initializing

int a,b,c,d,e,f;

...

followed like above post ^_^
Last edited on
closed account (zb0S216C)
[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]

Wazzak
Last edited on
Right, what I mean by "efficient" is neat looking and more user friendly to type.
closed account (zb0S216C)
Well, that depends on your style. Also, why would the user care? They aren't going to see the internals. Personally, I find this neat and efficient:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main( )
{
    std::cout << "\t Welcome, I will try to guess the minimum and maximum\n";
    std::cout <<  "\t           numbers of your chosen numbers.\n";

    std::cout << "Please enter 5 numbers: " << std::endl;

    int const max_no(5);
    int input_values[max_no] = {0};
    for(int i(0); i < max_no; ++i)
        std::cin >> input_values[i];

    std::cout << "Your chosen numbers: ";
    for(int i(0); i < max_no; ++i)
        std::cout << input_values[i] << " ";

   return(0);
}
@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.
closed account (zb0S216C)
codewalker wrote:
"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.

Wazzak
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
Topic archived. No new replies allowed.