C++! Can someone please explain me the reason of the output? 1) Suppose that the
ID: 3811198 • Letter: C
Question
C++! Can someone please explain me the reason of the output?
1) Suppose that the content of the input file is: 14 13 26 8 -11 36 0 -1. What is the output of the following code?
ifstream myInfile;
myInfile.open("input.txt");
int value;
int total = 0;
bool flag = true;
while (flag)
{
myInfile >> value;
if (value == -1)
{
flag = false;
continue;
}
total = total + value;
}
cout << total;
2) Suppose that the content of the input file is: 14 13 26 8 -11 36 0 -1. What is the output of the following code?
ifstream myInfile;
myInfile.open("input.txt");
int x;
int sum = 0;
myInfile >> x;
while (myInfile)
{
sum = sum + x;
myInfile >> x;
}
cout << sum << endl;
Explanation / Answer
Question 1
Answer: 86
All numbers in file will get added except the -1. When -1 value has come, loop will terminate
Question 2
Answer: 85
This time all numbers in the file will get added including -1. so output would be 85.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.