Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Mark the following statements as true or false. a. The extraction operator >>

ID: 3563004 • Letter: 1

Question

1. Mark the following statements as true or false.

a. The extraction operator >> skips all leading whitespace characters when

searching for the next data in the input stream.

b. In the statement cin >> x;, x must be a variable.

c. The statement cin >> x >> y; requires the input values for x and y to

appear on the same line.

2. Suppose num1 and num2 are int variables and symbol is a char variable.

Consider the following input:

47 18 * 28 $

What value (if any) is assigned to num1, num2, and symbol after each of the

following statements executes? (Use the same input for each statement.)

a. cin >> num1 >> symbol >> num2;

b. cin >> symbol >> num1 >> num2;

c. cin >> num1;

cin.get (symbol);

cin >> num2;

d. cin >> num1 >> num2;

cin.get (symbol);

e. cin.get (symbol);

cin >> num1 >> num2;

3. Suppose x and y are int variables and z is a double variable. Assume the

following input data:

37 86.56 32

What value (if any) is assigned to x, y, and z after each of the following

statements executes? (Use the same input for each statement.)

a. cin >> x >> y >> z;

b. cin >> x >> z >> y;

c. cin >> z >> x >> y;

4. Suppose x and y are int variables and symbol is a char variable. Assume

the following input data:

38 26 * 67 33

24 $ 55 # 34

# & 63 85

What value (if any) is assigned to x, y, and symbol after each of the

following statements executes? (Use the same input for each statement.)

a. cin >> x >> y;

cin.ignore(100, ' ');

cin >> symbol;

b. cin >> x;

cin.ignore(100, '*');

cin >> y;

cin.get(symbol);

c. cin >> y;

cin.ignore(100, ' ');

cin >> x >> symbol;

d. cin.get(symbol);

cin.ignore(100, '*');

cin >> x;

cin.ignore(100, ' ');

cin >> y;

e. cin.ignore(100, ' ');

cin >> x >> symbol;

cin.ignore(100, ' ');

cin.ignore(100, '&');

cin >> y;

5. Given the input:

46 A 49

and the C++ code:

int x = 10, y = 18;

char z = '*';

cin >> x >> y >> z;

cout << x << " " << y << " " << z << endl;

What is the output?

Explanation / Answer

1)

a) True

b) True

c) False

-------------------------------------------------------------------------------------------------------------------------------

2)

int variables are ‘num1’, ‘num2’.

char variable is ‘symbol’ .

input: 47 18 * 28 $

a)

cin>>num1>>symbol>>num2;

After above statement executes, the values stored in variables:

num1 =47

symbol =1

num2 =8

b)

cin>>symbol>>num1>>num2;

After above statement executes, the values stored in variables:

symbol =4

num1 =7

num2 =18

c)

cin>>num1;

cin.get(symbol);

cin>>num2;

cin.get(symbol):It is used to read character '' including white space from input stream.

After 47 it reads white space.

After above statement executes, the values stored in variables:

num1 =47

symbol = ' '

num2 =18

d)

cin>>num1>>num2;

cin.get(symbol);

cin.get(symbol):It is used to read character ''   including white space from input stream. After reading of 47, 18 space it reads.

After above statement executes, the values stored in variables:

num1 =47

num2 =18

symbol = ' '

e)

cin.get(symbol);

cin>>num1>>num2;

cin.get(symbol):It is used to read character '4'first line of input.

After above statement executes, the values stored in variables:

symbol = 4

num1 =7

num2 =18

-----------------------------------------------------------------------------------------------------------------------------------------------

3)

a)

x = 37, y = 86, z=0.56

b)

x = 37, y= 32, z = 86.56

c)

x = 86, z = 37.0

y was not assigned with any value. As a result, it will print a garbage value.

-------------------------------------------------------------------------------------------------------------------------------

4)

x, y are int variables.

symbol is char variable.

Input:

38 26 * 67 33

24 $ 55 # 34

# & 63 85

a)

cin>>x>>y;

cin.ignore(100,' ');

cin>>symbol;

cin.ignore(100,' '): It ignores next upcoming 100 characters or all characters until new line character found.

Above statement executes, the values stored in variables:

x     =38

y    =26

symbol=2

b)

cin>>x;

cin.ignore(100,'*');

cin>>y;

cin.get(symbol);

cin.ignore(100,'*'): It ignores next upcoming 100 characters or all characters until * character found. It reads blank.

Above statement executes, the values stored in variables:

x     =38

y    =67

symbol=' '

c)

cin>>y;

cin.ignore(100,' ');

cin>>x>>symbol;

cin.ignore(100,' '): It ignores next upcoming 100 characters or all characters until new line character found.

After above statement executes, the values stored in variables:

x     =24

y    =38

symbol=$

d)

cin.get(symbol);

cin.ignore(100,'*');

cin>>x;

cin.ignore(100,' ');

cin>>y;

cin.get(symbol):It is used to read character '3' from input stream.

cin.ignore(100,'*'): It ignores next upcoming 100 characters or all characters until * character found.

Read x after the * symbol found i.e., 67.

cin.ignore(100,' '): It ignores next upcoming 100 characters or all characters until new line character found.

Read 24 from next line in y.

After above statement executes, the values stored in variables

x     =67

y    =24

symbol=3

e)

cin.ignore(100,' ');

cin>>x>>symbol;

cin.ignore(100,' ');

cin.ignore(100,'&');

cin>>y;

cin.ignore(100,' '): It ignores next upcoming 100 characters or all characters until new line character found.

Read 24 from next line in x.

cin.ignore(100,'&'): It ignores next upcoming 100 characters or all characters until & character found.

Read 63 from next line after & symbol in y.

After above statement executes, the values stored in variables

x     =24

y    =63

symbol=$

-------------------------------------------------------------------------------------------------------------------------------------------

5)

Given the input is “46 A 49”.

As expected, the first input 46 will be read into the variable x.

The second input value is the character ‘A’. The cin statement tries to input this character into the variable y. However because y is an integer variable, the input stream enters a fail state. Therefore the values of y, z will be unchanged i.e. y=18 and z =’*’.

Therefore the output of the program is: 46 18 *