Can the following two lines be condensed into one?
int foo;
std::cin >> foo;
Variables can either be initialized in the same statement as the declaration or later in the code.
Example - Declaring multiple variables in a statementIf your variables are the same type, you can define multiple variables in one declaration statement. For example: int age, reach; In this example, two variables called age and reach would be defined as integers.
int a = 20; int b; The variable a is initialized with a value in the program while the variable b is initialized dynamically.
The smart-ass answer:
int old; std::cin >> old;
The horrible answer:
int old, dummy = (std::cin >> old, 0);
The proper answer: old
has to be defined with a declaration before it can be passed to operator>>
as an argument. The only way to get a function call within the declaration of a variable is to place it in the initialization expression as above. The accepted way to declare a variable and read input into it is as you have written:
int old;
std::cin >> old;
You can... with
int old = (std::cin >> old, old);
but you really should not do this
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With