(1) If you are reading a character, like char someChar , what is the difference
ID: 3764659 • Letter: #
Question
(1)
If you are reading a character, like char someChar, what is the difference between
cin.get(someChar);
and
cin >> someChar
?
Select one:
a. all of the choices (except 'none')
b. get() skips leading whitespace.
c. get() can get a whole string.
d. "cin >>" skips leading whitespace.
e. You can't use get() like this.
f. "cin >>" can't read just a single character.
g. none of the other choices
h. They are the same!
(2)
With this as your input
abcd e f g
hij k l
and this code
char ch1, ch2;
cin >> ch1;
cin.ignore(12, ' ');
cin >> ch2;
What is the value of ch2 ?
(3)
Which of the following do you have to include to read data from a file?
Select one:
a. ostream
b. istream
c. fstream
d. stream
e. cmath
f. cin
g. iostream
h. iomanip
i. cout
(4)
Imagine that the user has entered the following text into the console:
160.34 97 104
What are the values of a, b, and c after the following code executes? Pay careful attention to the types of the variables.
int a;
Select one:
a. a = 160.34, b = '97', c = 104
b. a = 160, b = '.', c = 34
c. a = 160, b = '9', c = 7
d. a = 1, b = '6', c = 0
(5)
Given this input
10.25
and this code reading that input
int i;
float f;
cin >> i >> f;
cout << i << ", " << f << ' ';
What would be output for f ? (enter 'invalid' for either nothing or invalid code)
(6)
With this as your input
abcd e f g
hij k l
and this code
char ch1, ch2;
cin >> ch1;
cin.ignore(2, ' ');
cin >> ch2;
What is the value of ch2 ?
(7) What is the standard istream that you read input from?
(8)
Given this input
CS1044 Programming in C++
and this code reading that input
string name;
getline(cin, name);
cout << name << ' ';
How many characters are stored in name ? (enter 'invalid' for either nothing or invalid code)
Explanation / Answer
(1) answer is d. - cin can skip white starting white spaces but get() cannot.
(2) the value of xh2 is 'h'. Since ignore is used before with 12 and - it means clear any values till the first input acceptance (in this case only 1 char) + leave 12 chars and take 13th, since 13 is not there, and was triggered (new line), it clear the before input and ch2 gets the next character H.
(3) Istream is used to read data, fstream is used for both read and write. Since the question is on read - we can go with Istream.
(4) Answer is b. a=160; b=.; c=34
INt stops when an unacceptable char is found, then b takes over for 1 char length, after that found int is 34 till space was found. Also note: in cin spaces are removed before inputing the values.
(5) F will be 0.25. Explanation same as provided for 4th question.
(6) Ch2= 'd'. Explanation is same as provided in question 2.
(7) the standard form is:
ifstream infile;
infile.open("afile.dat");
(8) Length is 25. We can use name.size() to give the count of chars in it.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.