Colour Theme Font Size Options
 
 
 
Exercise 2

EOF Loops Exercise

Create a C++ program ex2_iodemo.cpp with the following code in the Visual Studio project or copy the following code into your IDE:


This program uses file I/O to read floating point values from the data file inputfile.txt until the end of the file is reached. It then writes the sum and average of all the floating point numbers to the output file outputfile.txt.

Your assignment is to add the code to the while loop to read a number from the file, add it to the sum, and increment the counter. Look at the comments in the program to see where to put this code.

Instructions:

  • Complete the program according to the "Add code: " comments in the program.
  • Add 5 to 10 floating point numbers to a file called inputfile.txt. You may pick your own numbers. (You may have to create this file in your own IDE.)
  • Compile and run this C++ program. The output will be in the file outputfile.txt.

When you are done:

  • Take a screenshot of your IDE and console window after compiling and running the program.
  • For this exercise, you will have: ex2_iodemo.cpp, inputfile.txt and outputfile.txt files.