Suppose x and y are int variables and symbol is a char variable. Assume the foll
ID: 3821748 • Letter: S
Question
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;
Explanation / Answer
Only thing to note here is the usage of cin.ignore function:
When we take input using cin and hit enter, then a ' ' character gets saved in the stream. So, if we use getline to retrieve a char, getline will check the stream and will found ' ' character and store it in the char variable. To ignore this ' ' character we use cin.ignore function with ' ' as second argument.
In above statements, cin.ignore(100,' ') means skip the next 100 characters or skip the characters until a ' ' character is found.
Output of the above statements
a. cin >> x >> y;
cin.ignore(100, ' ');
cin >> symbol;
Values are
b. cin >> x;
cin.ignore(100, '*');
cin >> y;
cin.get(symbol);
Values are
c. cin >> y;
cin.ignore(100, ' ');
cin >> x >> symbol;
Values are
d. cin.get(symbol);
cin.ignore(100, '*');
cin >> x;
cin.ignore(100, ' ');
cin >> y;
Values are
e. cin.ignore(100, ' ');
cin >> x >> symbol;
cin.ignore(100, ' ');
cin.ignore(100, '&');
cin >> y;
Values are