Ok, I have made a lot of "progress" but I am running into problems everywhere. I got the program to work from a single source file (without the class). Then I began moving stuff to the class and trying to get it to work. The first problem is that I have removed the array from the class but this does not leave any real data in the class. The class is now just a collection of member functions to manipulate the array. This doesn't seem right so I must assume that there is a good way to read the input file from the header file or "pass" the input file to the class header. Also, I cannot get the header file to recognize the output file to write the output even though I have moved that definition to above the int main() (this is how I thought a global variable is declared). I have much to learn if anyone can teach me. Sorry for the length of this post but there are three files involved (not counting the input and output files which can be simulated with any collection of integers). Here goes:
Source.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cstdlib>
#include "datalist.h"
using namespace std;
void swap(int, int);
struct CommandLineException
{
CommandLineException(int max, int actual)
{
cout << endl << "Too many command line arguments." << endl;
cout << "A maximum of " << max << " arguments are permitted." << endl;
cout << actual << " arguments were entered." << endl;
}
};
struct FileException
{
FileException(char* fn)
{
cout << endl << "File " << fn << " could not be opened." << endl;
}
};
char ifn[255], ofn[255];
ifstream j(ifn);
ifstream i(ifn);
ofstream o(ofn);
int main(int argc, char* argv[])
{
if(!j) throw FileException(ifn);
if(!i) throw FileException(ifn);
if(!o) throw FileException(ofn);
try
{
switch(argc)
{
case 1:
cout << "Enter the input file name. ";
cin >> ifn;
cout << "Enter the output file name. ";
cin >> ofn;
break;
case 2:
strcpy(ifn, argv[1]);
cout << "Enter the output file name. ";
cin >> ofn;
break;
case 3:
strcpy(ifn, argv[1]);
strcpy(ofn, argv[2]);
break;
default:
throw CommandLineException(2, argc - 1);
break;
}
int* counta = new int;
*counta = 0;
int testmsg;
while (j >> testmsg)
{
*counta++;
}
const int SIZE = *counta;
delete counta;
int* data = new int[SIZE];
int* countb = new int;
*countb = 0;
while (i >> data[*countb])
{
*countb++;
}
delete countb;
o << "The input file originally contained values in this order: \n";
/* for (int a = 0; a < SIZE; a++)
{
o << data[a] << " ";
}*/
datalist info(data, SIZE);
info.print(data, SIZE);
o.close();
i.close();
delete[] data;
}
catch(...)
{
cout << "Program terminated." << endl;
exit(EXIT_FAILURE);
}
return 0;
}
void swap(int& x, int& y)
{
int temp = x;
x = y;
y = temp;
}
header.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cstdlib>
using namespace std;
#include "datalist.h"
datalist::datalist(int data[], int size)
{
data = new int[size];
}
void datalist::bubbleSort(int data[], int size)
{
for (int i = 0; i < size-1; i++)
{
bool swapped = false;
for (int k = 0; k < size-1-i; k++)
{
if (data[k] > data[k+1])
{
swap(data[k], data[k+1]);
swapped = true;
}
}
if (!swapped) break; // all sorted
}
}
void datalist::print(int data[], int size)
{
for (int i = 0; i < size; i++)
o << data[i] << " ";
o << endl;
}
header.h
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cstdlib>
using namespace std;
#ifndef datalist_h
#define datalist_h
class datalist
{
private:
public:
datalist(int[], int);
void print(int[], int);
void bubbleSort(int[], int);
};
#endif
PS. I have a feeling that some of the stuff that seems to be working is not correct either, i.e., I had to create a constructor which is declaring a new dynamic array but I have only one delete[]. Also, I am sure that the header.cpp and header.h don't need all of the include files (or the main for that matter). Anyways, any problems you can findplease help me understand the way it is supposed to work so that next time someone asks, it won't be me.