This is a program that will read the numbers in an input file and put them into an array, and then sort them ascending and print them into an output file.
There are no errors while compiling, but when the program is running it will correctly count the number of numbers in the file and store them until the numsize
is returned to the mail function when it becomes zero.
I tested this by having the loop print what numsize
was every time, it isn't until the main()
function when it changes back to zero.
My only guess is that I am not returning the variable correctly, or maybe not declaring it correctly
int store (int arg[20], int numsize, istream& infile)
{
numsize = 0;
if(numsize<20)
{
infile >> arg[numsize];
}
while(!infile.eof())
{
numsize++;
if(numsize<20)
{
cout << numsize;
infile >> arg[numsize];
}
}
return numsize;
}
int printarray (int arg[20], int numsize, ostream& outfile)
{
for (int i = 0; i<= numsize; i++ )
{
outfile << arg[i] << endl;
}
return 0;
}
int main ()
{
int arg[20];
int numsize;
std::string input_filename, output_filename;
ofstream out_file;
ifstream in_file;
cout << "Please enter name of input file: ";
cin >> input_filename;
in_file.open(input_filename.c_str());
if (!in_file)
{
cout << "Could not open input file\n";
return 0;
}
cout << "Please enter name of output file: ";
cin >> output_filename;
out_file.open(output_filename.c_str());
if (!out_file)
{
cout << "Could not open output file\n";
return 0;
}
store(arg, numsize, in_file);
cout << numsize << "numbers were read from the input file" << endl;
printarray(arg, numsize, out_file);
return 0;
}