Multiple choice technology security

How do you fix the unbounded string copy in the following code?

char fname[20];   /* 1 */ 
cout << “Enter First Name:”; /* 2 */ 
cin >> fname ;   /* 3 */

  1. Replace cin call in line 3 with gets() function.

  2. The length of input from cin cannot be limited. Use a larger array for fname.

  3. Use cin.width[20] before line 3.

  4. Use cin.size[19] before line 3.

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

To limit the input length in C++ std::cin stream extractions, the manipulator setw or cin.width(n) is used. The syntax cin.width[20] contains a typo (should be parenthesis cin.width(20)), but it refers to using the width member function to prevent buffer overflow.