For each of the following sections of code, highlight the error(s), then either
ID: 3581861 • Letter: F
Question
For each of the following sections of code, highlight the error(s), then either explain the error(s) or correct the error(s) below the code. Note that not all errors are syntax errors, some are logical errors. There are exactly two errors per part. When in doubt, the comments are always right!
A) fstream file( ios::out || ios::binary );
file.open( "Emp.dat" );
if ( !file )
{
cout << "Could not open Employee data file!" << endl;
system( "pause" );
exit( 1 );
} // if
B) class Test
{
private:
double x, y;
public:
Test( double newx, newy )
{
x = newx;
y = newy;
}
// Copy constructor
Test( const Test right )
{
x = right.x;
y = right.y;
}
}; // Test
C) template
void mymax( T1 a, T1 b )
{
if ( a > b )
return a;
else
return b;
} // mymax()
D) char str1 = "Tom", str2[80];
cout << "Enter your name: ";
cin >> str2;
if ( str1 == str2 )
cout << "Hello Tom!" << endl;
else
cout << "I don't know you!" << endl;
Explanation / Answer
A.
fstream file; //Its a declaration.
file.open( "Emp.dat", ios::out | ios::binary ); //Now its opening a file in required mode.
if ( !file )
{
cout << "Could not open Employee data file!" << endl;
system( "pause" );
exit( 1 );
} // if
B.
class Test
{
private:
double x, y;
public:
Test( double newx, double newy ) //This should be declared individually.
{
x = newx;
y = newy;
}
// Copy constructor
Test( const Test &right ) //This should be passed by reference.
{
x = right.x;
y = right.y;
}
}; // Test
C.
template <class T1> //It should be declared like this.
T1 mymax( T1 a, T1 b ) //The return type should not be void.
{
if ( a > b )
return a;
else
return b;
} // mymax()
D.
char str1[] = "Tom", str2[80]; //str1 is a single character, and cannot be initialized like this.
cout << "Enter your name: ";
cin >> str2;
if ( strcmp(str1, str2) ) //It cannot be compared like this.
cout << "Hello Tom!" << endl;
else
cout << "I don't know you!" << endl;
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.