Sunday, 5 October 2014

Identify and correct the errors in each of the following

Leave a Comment

















Identify and correct the errors in each of the following:
  1. while ( c <= 5 )
    {
               product *= c;
               ++c;
  2. cin <<value;
  3. if ( gender == 1 )
          cout << "Woman" << endl;
    else;
          cout << "Man" << endl;

Solution


  1. Error: Missing the closing right brace of the while body.
    Correction: Add closing right brace after the statement ++c;
  2. Error: Used stream insertion instead of stream extraction.
    Correction: Change << to >>.
  3. Error: Semicolon after else results is a logical error. The second output statement will always be executed.
    Correction: Remove the semicolon after else.
If You Enjoyed This, Take 5 Seconds To Share It