Write a code fragment that adds up all the data in a file of numbers accessed by ifstream variable in and displays the result. Stop adding numbers when bad data or end of file is encountered. Hint: use a while loop.

What will be an ideal response?



sum = 0;
in >> num;
while (!in.fail()) {
sum += num;
in >> num;
}
cout << "Sum of data = " << sum << endl;

Computer Science & Information Technology

You might also like to view...

An object can be ________ vertically or horizontally for a mirror image of the object

Fill in the blank(s) with correct word

Computer Science & Information Technology

A computer forensic specialist may examine the ________ to ascertain the true identity of a file

Fill in the blank(s) with correct word

Computer Science & Information Technology

In a(n) ____ loop the condition is tested at the end of the repeating section of code.

A. pretest B. posttest C. fixed-count D. condition-controlled

Computer Science & Information Technology

What modern encryption technology is especially useful for protecting data stored on laptops?

What will be an ideal response?

Computer Science & Information Technology