HELP..I am completely lost. I have to write a program that reads from a file called DATAFILE2.TXT

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.
Last edited on
Doesn't sound like your Day 1 project, which part are you lost on ?

Here is what I think the starting format should look like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <string>
#include<fstream>
using namespace std;

ifstream infile;
ofstream outfile;
void StepA()
{
   infile.open("DATAFILE2.TXT");
   outfile.open("ANSWERS.TXT");
   cout << "A) Read the file and write all of the integers to the output file in one line." << endl;
   infile.close();
   outfile.close();
}

void StepB()
{
cout << "B) "<< endl;
}

void StepC()
{
cout << "C) "<< endl;
}

void StepD()
{
cout << "D) "<< endl;
}

void StepE()
{
cout << "E) "<< endl;
}

void StepF()
{
cout << "F) "<< endl;
}

void StepG()
{
cout << "G) "<< endl;
}

void StepH()
{
cout << "H) "<< endl;
}

int main()
{
StepA();
StepB();
StepC();
StepD();
StepE();
StepF();
StepG();
StepH();

return 0;
}
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..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <fstream>
#include <string>
using namespace 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
Last edited on
Topic archived. No new replies allowed.