I am completely lost. I have to write a program that reads from a file called DATAFILE2.TXT, performs the following tasks, and writes the results of the task to an output file called ANSWERS.TXT:
NOTE: there are no given integers
A) Read the file and write all of the integers to the output file in one line.
B) Reread the file and compute the average of all of the integers in the file.
Output the average as a floating-point number with 3 decimal positions.
C) Reread the file and compute the average of the first 12 integers in the file.
D) Reread the file and compute the sum of every third integer in the file. That is, compute the sum of the third, sixth, ninth, etc. integers.
E) Reread the file and determine the range of the integers in the file. That is, find the smallest and largest integers.
F) Reread the file and find the integers in the file that is closest to 200
G) Reread the file and compute the average of all integers on the first two lines.
Output the average as a floating-point number with 3 decimal positions.
H) Reread the file and compute the sum of the integers in the file as long as the sum does not exceed 100. Use a flag controlled loop structure.
Each of the tasks should be performed independently from the other tasks. Do not combine the tasks.
Each time you need to reread the input file, you must close and open it again.
first whenever you see yourself copying and pasting as you have stop and consider a loop. secondly you have opened and closed the file outside of main so there is no way for main to call any of the functions you have called. you do not need to call as many functions as you have to complete your goal..
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
int main()
{
ifstream infile; //input the file infile is your new filename
int number; // counter variable storing your text from file
string filename;
cout << "Enter the filename: " ;
cin >> filename;
cout << "\n";
infile.open(filename.c_str())
if (!infile)
{
cout << "Error opening file.\n";\
exit ( 1 );//exit program if there is an error
}
while ( infile >> number)
{
cout << number << endl;
}
return 0;
}
this will display every value from a .txt file. also make sure that if you are using VS that you have the txt file in your resource file in your Solution Explorer