Exercise 2.9: Explain the following definitions. For those that are illegal, explain what’s wrong and how to correct it.
(a) std::cin >> int input_value;
(b) int i = { 3.14 };
(c) double salary = wage = 9999.99;
(d) int i = 3.14;
answers:
(a)error, variable must be declare before use.
correction: int input_value; std::cin >> input_value;
(b)error: type ‘double’ cannot be narrowed to ‘int’ in initializer list
correction: int i = {3};
(c)error: use of undeclared identifier ‘wage’
correction: double wage; double salary = wage = 9999.99;
(d)correct: but the fractional part will be narrowed. the value will be int 3.
Exercise 2.10: What are the initial values, if any, of each of the following variables?
std::string global_str;
int global_int;
int main()
{
int local_int;
std::string local_str;
}Answers:
the initial value of global_str is empty;
the initial value of global_int is 0;
the initial value of local_str is empty;
the initial value of local_int is -348039344