C++ Programming

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;

(a) ‘int’ will not be defined after cin. It is illegal. The correct way is

 int input_value = 0;

 std::cin>>input_value;

(b) 3.14 is a float value, so it will not initialize an integer value. It is illegal. The correct way is 

float i = {3.14};

(c) It is illegal. ‘wage’ is not declared in this scope. The correct one is 

double wage = 9999.99;

double salary = wage;

(d)  It will not show any error. But the value will be truncated. It will display a value of 3. So it is illegal. The correct way is 

float i = 3.14;

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments