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