cin Worksheet Answers

Assume the following declarations:

     int a, b, c;
     float x, y, z;

For each of the following, tell what value, if any, will be assigned to each of these variables, or explain why an error occurs. Use the given input data:

  1. cin >> a >> b >> c
        >> x >> y >> z;
    Input:
    1   2   3
    4 5.5 6.6
    Result:
    a = 1  b = 2  c = 3
    x = 4  y = 5.5  z = 6.6
    
  2. cin >> a >> b >> c;
    cin >> x >> y >> z;
    Input:
    1
    2
    3
    4
    
    5.5
    6.6
    Result:
    a = 1  b = 2  c = 3
    x = 4  y = 5.5  z = 6.6
    
  3. cin >> a >> x;
    cin >> b >> y;
    cin >> c >> z;
    Input:
    1 2.2
    3 4.4
    5 6.6
    Result:
    a = 1  b = 3  c = 5
    x = 2.2  y = 4.4  z = 6.6
    
  4. cin >> a >> b >> c;
    cin >> x >> y >> z;
    Input:
    1 2.2
    3 4.4
    5 6.6
    Result:
    a = 1  b = 2  c, x, y, z unpredictable
    cin reads 2 for b, then .2 for c, which is invalid, so error
    no reads are done for x, y, and z after error on c
  5. cin >> a;
    cin >> b >> c;
    cin >> x >> y;
    cin >> z;
    Input:
    1   2   3
    4 5.5 6.6
    Result:
    a = 1  b = 2  c = 3
    x = 4  y = 5.5  z = 6.6
    
  6. cin >> a
        >> b >> c
        >> x >> y
        >> z;
    Input:
    1   2   3
    4 5.5 6.6
    Result:
    a = 1  b = 2  c = 3
    x = 4  y = 5.5  z = 6.6
    
  7. cin >> a >> b;
    cin >> c >> x >> y >> z;
    Input:
    1   2   3   4 5.5
    6.6 7 8.8 9.9  10
    Result:
    a = 1  b = 2  c = 3
    x = 4  y = 5.5  z = 6.6
    

Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2004