These two questions are from a practice exam I took. The * denotes the correct answer.
14. What does the following print? Assume all the appropriate header files
(infile.txt = 1 2 3 4)
ifstream infile;
infile.open("infile.txt");
int num = 0, sum = 0;
while (infile) {
infile >> num;
sum += num;
}
cout << sum;
A) 6
B) 10
* C) 14
D) none of the above
15. If infile.txt holds
1 2 3 4
What does the following print? Assume all the appropriate header files
ifstream infile;
infile.open("infile.txt");
int num = 0, sum = 0;
while (infile >> num) {
sum += num;
}
cout << sum;
A) 6
* B) 10
C) 14
D) none of the above
------------
I keep getting 10 for 14. Sum starts at 0, then goes up by 1, then gets 2, 3, 4 added to it, which is 10. Can someone tell me what I am doing wrong.
For number 15. Why is this one ten. I picked none of the above because I feel the while (infile >> num) is illegal, due to the >>.
I figured out 15. I thought the while (infile >> num) was illegal, but it is fair game. Still can anyone help me with 14? Exam is tomorrow and I still don't get that one.