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

The 'cin >>' operator is unbounded and can cause buffer overflows. Using 'cin.width(n)' or the 'setw(n)' manipulator limits the number of characters read into the buffer, ensuring it does not exceed the array size.