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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
// main.cpp
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include "DynamicArray.h"
using namespace std;
// prototype local functions
// this works, but I deleted it because my submission was too long.
void toContinue(); // used to hold data on screen until enter key
// let me know if you want the files.
bool testOpenFile(ifstream& dSource, string); // checks if file exists and opens it
// begin main function
int main(int argc, char* argv[])
{
// default file names so I don't have to type it every time.
string inFILE_a = "C:\\Users\\Chris\\Desktop\\C++\\Program 5\\data5i.dat";
string inFILE_b = "C:\\Users\\Chris\\Desktop\\C++\\Program 5\\data5j.dat";
ifstream dSa; // name the file stream variable
ifstream dSb; // name the file stream variable
// receives file names if they are entered at the command line.
if (argc > 1)
{
inFILE_a = argv[1];
inFILE_b = argv[2];
}
// test if file exists and open it for reading
if (!(testOpenFile(dSa, inFILE_a) && testOpenFile(dSb, inFILE_b)))
{
return 1; // data source failed, exit main, program stop.
}
else
{
DynamicArray da1; // create a dynamic array object, initial size is 10
DynamicArray da2; // create a dynamic array object, initial size is 10
// Do some work in here to load data into the dynamic arrays, well tested, works.
DynamicArray da3; // create another empty array
da3 = da1; // This appears to work
// change a few values
da1[3] = 14; // use works just like a regular array
da2[3] = 16; // use works just like a regular array
da3[3] = 24; // use works just like a regular array
da3.remove(5); // works like it is supposed to.
cout << "\nThe elements in array 1 are: " << endl;
// I need to overload << to work here but made this .print as a workaround
da1.print(); // this works but doesn't fill the requirement
cout << "\nThe sum of all elements in array 1 is ";
cout << da1.sum() << endl; // works, returns the sum and that prints.
cout << "\nThe current size of array 1 is ";
cout << da1.size() << endl; // works, returns the size and that prints.
// only did these to validate that there are three separate sets of data
cout << "\nThe elements in array 2 are: " << endl;
da2.print();
cout << "\nThe sum of all elements in array 2 is " << da2.sum() << endl;
cout << "\nThe current size of array 2 is " << da2.size() << endl;
cout << "\nThe elements in array 3 are: " << endl;
da3.print();
cout << "\nThe sum of all elements in array 3 is " << da3.sum() << endl;
cout << "\nThe current size of array 3 is " << da3.size() << endl;
// this crashes the compiler -
da3 = da1 + da2; // I want da3 to be all the elements in da1 and da2 no math.
}
// open the input file, write an error if it fails
bool testOpenFile(ifstream& dSource, string fileName)
{
dSource.open(fileName.c_str()); // allows define of file name at start of code
if (dSource.fail())
{
cout << "Couldn't open input file. File not found." << endl;
toContinue();
return false; // Exit with an error if the input file does not exist.
}
return true;
}
|