Assume the following stream input and variable declarations
Assume the following stream input and variable declarations: Stream Input (from cin): 42 75 B 19 Q 22 Declarations: int x, y; char ch; What is the result for the following statements, i.e. what values are stored in which variables after each statement is executed? Consider each part (a, b) by itself, that is, each set of instructions will be expected to interact with a fresh copy of the same stream input as given above. (a) cin >> x >> y; cin.ignore (50, ’\ ’); cin >> ch; (b) cin.get (ch); cin.ignore (50, ’B’); cin >> x >> y;
Solution
cin.ignore(x,\'\ \') will ignore the next characters or untill new line character is inserted.
a.) x will have 42, y will have 75 and ch will have garbage value
b.) cin.get(ch); takes 44 as input but it store only first character in it i.e., 4. and next statement cin.ignore(); ignore all the characters until we recieve \'B\' or new ine character. So, ch will have 4, x will have 19 and y will have default value or garbage value.
