Identify the errors (syntax or logical) in the following code segment. Select al
ID: 3810698 • Letter: I
Question
Identify the errors (syntax or logical) in the following code segment. Select all that apply.
const char *mutant = new char[10];
++mutant;
cin.getline(mutant, 9);
if (mutant == "Cyclops")
cout << mutant;
declaration of the variable mutant
checking if "Cyclops" is stored in mutant
moving mutant
using cin.getline to read data for mutant
printing out mutant
storing a value in mutant
Identify the errors (syntax or logical) in the following code segment. Select all that apply.
const char *mutant = new char[10];
++mutant;
cin.getline(mutant, 9);
if (mutant == "Cyclops")
cout << mutant;
Answers:declaration of the variable mutant
checking if "Cyclops" is stored in mutant
moving mutant
using cin.getline to read data for mutant
printing out mutant
storing a value in mutant
Explanation / Answer
Here there is not any syntax errors.
But the logical errors are there.
First is when mutant is declared. It is declared as const so in the program ahead, its value cannot be changed.
There is not any error in moving mutant.
There is 1 more error. Checking the value using ==. The == operator compares addresses of two strings rather than its content so logically it is correct.
So logically at first we need to do mutant-- then use strcmp from string.h file. These are 2 errors here.
So option (a) and (b) are the required answers.
Please comment if there is any query. Thank you. :)