How do I switch program to use a txt file??

This is currently the program I have. I need to modify it so that i can read from a blank txt file and display the same information.
#include <iostream>
#include <iomanip>
using namespace std;

// Function Prototypes
int* create_array(int);
void enter_data(int*, int);
void show_array(int*, int);
float find_average(int*, int);


int main()
{

int* dyn_array;
int students;
float avrg;

do
{
cout << "How many students will you enter? ";
cin >> students;
} while (students <= 0);

dyn_array = create_array(students);
enter_data(dyn_array, students);
avrg = find_average(dyn_array, students);


cout << " " << endl;
cout << "The array is: " << endl;
show_array(dyn_array, students);

cout << endl;
cout << "The average is " << avrg << ".\n";

delete[] dyn_array;
return 0;
}

//************************************
//***** create_array() *****
// This function creates an array.
//************************************
int* create_array(int num)
{
int* array;
array = new int[num];
return array;
}

//*************************************
// *****enter_data() *****
// This function inputs data for all students
// into dynamically created array
//*************************************
void enter_data(int* dyn_array, int students)
{

for (int count = 0; count < students; count++)
{
cout << "How many movies did student ";
cout << (count + 1);
cout << " see? ";
cin >> *(dyn_array + count);
if (*(dyn_array + count) > 100) {
cout << "Must enter a number less than 100! Exiting Program.";
exit(0);
}
}

}

//*******************************************
// showArray()
// This function displays the array
// to the user
//*******************************************
void show_array(int* dyn_array, int students)
{
for (int index = 0; index < students; index++)
cout << endl << "Student " << (index + 1) << " saw " << *(dyn_array + index) << " movies.";
cout << "\n";
}

//*******************************************
// *****find_average() *****
// This function gets the average of the
// number of movies seen
//*******************************************
float find_average(int* dyn_array, int students)
{
float sum = 0;
float avrg;

for (int count = 0; count < students; count++)
{
sum += *(dyn_array + count);
avrg = sum / students;
}
cout << fixed << showpoint << setprecision(2);
return avrg;
}
you can't, by definition, read data from a blank text file.
but if you had a text file with something in it, you could replace all your cin statements with file <<
where file comes from
ifstream file ("yourfile.txt");

I highly recommend you STOP here, and write a tiny main program that reads a text file and play with it a few min to get familiar before trying to inject it into the above with no background. There are many examples of using ifstream here and on the web. Keep it simple for now.
Last edited on
A tutorial on reading/writing data with files:

http://www.cplusplus.com/doc/tutorial/files/
thanks jonnin for the advice i will give it a shot
Topic archived. No new replies allowed.