1.With this as your input abcd e f g hij k l and this code char ch1, ch2; cin >>
ID: 3552875 • Letter: 1
Question
1.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 ?
2.
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)
3.Given this input
Todd Stevens
and this code reading that input
int i=99;
double f=1000.1;
cin >> i >> f;
cout << i << ", " << f << ' ';
What would be output for f ? (enter 'invalid' for either nothing or invalid code)
ifstream inputFile("testData.dat");
is equivalent to this code
ifstream inputFile;
input.open("testData.dat");
True or False
With this as your input
ab
cde f
and this code
char ch1, ch2, ch3;What is the value of ch3 ?
cin >> ch1 >> ch2;
cin.get(ch3);
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.If you are reading a character, like char someChar, what is the difference between
cin.get(someChar);
and
cin >> someChar
?
Todd Stevens
and this code reading that input
string name;
getline(cin, name);
cout << name << ' ';
How may characters are stored in name ? (enter 'invalid' for either nothing or invalid code)
Explanation / Answer
1.
Input: abcd e f g hij k l
The given program picks a character value from the standard input and stores it in the variable ch1. Then the statement cin.ignore(12, ' '); ignores/skips 12 characters (including whitespaces) from the standard input.
The 12 characters are skipped after storing the first character in the variable ch1. The variable ch1 contains the value a.
position
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
value
a
b
c
d
e
f
g
h
i
j
k
l
The first 13 characters get skipped and the character stored in the variable ch2 is j.
2.
Input: 10.25
The statement cin>>i picks an integer value from the standard input and stores it in the variable i. Hence, the integral part of the input, 10, gets stored in the variable i.
The variable f is of float type and the statement cin>>f stores a floating-point value from the standard input in the variable f. Hence, the value stored in the variable f is 0.25.
Therefore, the output for f is 0.25.
3.
Input: Todd Stevens
The variables i and f stores an integer and a floating-point value, respectively. The statement cin>>i>>f picks an integer and a float value from the standard input and stores them in the respective variables. Since, the input string does not contain any numeric value, the values of the variables i and f remain unchanged.
Therefore, the output for f is 1000.1. Hence, the option b is correct.
4.
The first statement creates an object inputFile of the ifstream class and opens a binary file named “testData.dat”.
The second statement creates an object inputFile of the ifstream class. Then the file “testData.txt” is opened using the object named input which is undefined.
Hence, the above code statements are not equivalent and the given statement is false.
5.
Input:
ab
cde f
The statement cin>>ch1>>ch2 stores the first 2 characters (a and b) from the standard input in the variables ch1 and ch2 respectively.
The statement cin.get(ch3) stores a character (including whitespaces) from the standard input in the variable ch3. The next character after the first 2 characters (a and b) is ‘ ’.
Hence, the value of the variable ch3 is ‘ ’.
6.
Input:
abcd e f g
hij k l
The statement cin>>ch1 stores the first character from the standard input in the variable ch1. The value stored is a.
Then the statement cin.ignore(2, ‘ ’) skips the next 2 characters from the input stream. The statement cin>>ch2 stores the next character d in the variable ch2.
Hence, the variable ch2 contains the value d.
7.
The cin statement reads the stream of characters from the standard input excluding the whitespaces. The cin.get() function is used to read a single character from the standard input including the whitespaces.
Hence, the correct option is b.
8.
Input: Todd Stevens
The statement getline(cin, name) reads a string of characters from the standard input until the next line character is found and stores it in the variable name. The next line character is not stored in the variable.
Hence, the number of characters stored in the variable name is equal to the length of the input string = 12.
position
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
value
a
b
c
d
e
f
g
h
i
j
k
l
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.