/********************************************************************************************
example 10 - readint.cpp
read integers into an array, then display them in either direction
********************************************************************************************/
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
#include "../ex09/safearray.h"
//prototypes
void readFrom(string file, safearray&);
void showForward(const safearray&);
void showBackward(const safearray&);
//------- The MAIN point of entry -------
void main() {
safearray Array;
string filename;
cout << "What is the file to read: ";
cin >> filename;
readFrom(filename, Array);
cout << "Total Elements Used: " << Array.count() << endl
<< "Out of a maximum of: " << Array.maxsize << endl;
showForward(Array);
showBackward(Array);
}
//------- Load a list of integers from a text file -------
void readFrom(string file, safearray& list) {
ifstream fin(file.c_str());
for (int i = 0; i < maxsize; i++) {
int x;
if(fin >> x)
list[i] = x;
else
break;
}
}
//------- Display the list of integers in the order they are stored -------
void showForward(const safearray& list) {
cout << "Forward:" << endl;
for (int i = 0; i < list.count(); i++)
cout << list[i] << endl;
cout << endl;
}
//------- Display the list of integers in the reverse of the order they are stored -------
void showBackward(const safearray& list) {
cout << "Backward:" << endl;
for (int i = list.count(); i > 0; i--)
cout << list[i-1] << endl;
cout << endl;
}