Pre-Lab - Lab (11)



Answer the following questions in writing.

1) In Lab(5), you used an array of characters to store file names.  You used:

char inp_file[15], out_file[15];

You learned to read these two names from the keyboard and then use the names to connect streams to them.  Can you explain a way to check and see whether the inp_file and out_file name are the same?  Write the C++ code segment that does the checking.
 
 
 
 
 

2) Can you tell whether there is any difference between the following two statements?

char my_string[] = "Hello";
char my_string[] ={'H', 'e', 'l', 'l', 'o'};
 
 
 

Suppose I define two character arrays in my program as:

char first_array[15] = "Hello";
char second_array[6] = "Dear";
 

Write the C++ code segment to modify the first array so that it contains its contents plus the contents of the second array? i.e., Hello Dear
 

3) The following code segment causes a run-time error.  Explain what the error is and suggest a solution to fix the problem.

int main( )
{
     char a_string[8];

      for(i = 0; i < 9; i++)
      {
             cout << "Enter a character \n";
             cin >> a_string[i];
       }

       return 0;
}
 
 
 
 

4) Could you change the size of the array while the program was running to avoid the error? We can use vectors to do that... how?  Soon you will learn.