### 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
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364`` ``````#include #include #include 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..
 ``123456789101112131415161718192021222324`` ``````#include #include #include 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.