Reputation: 153
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;
}
Upvotes: 2
Views: 1294
Reputation: 1877
When you are calling the store
function in the main function, then try to save the value of the returned variable numsize
from the function store
. You may try the following code:
numsize = store(arg, numsize, in_file);
Upvotes: 2
Reputation: 311048
You should write at least like
numsize = store(arg, numsize, in_file);
It would be more correct to declare the function like
size_t store ( int arg[], size_t n, istream& infile);
where the parameter n
denotes the number of elements in the array. Otherwise the function will be depend on the magic unknown number 20.
Also the loop in the function is incorrect. The function can place into the array the last number twice because the condition eof
is checked after a number is stored in the array.
The function implementation can look like
size_t store( int arg[], size_t n, istream& infile )
{
size_t i = 0;
int value;
while( i < n && infile >> value ) arg[i++] = value;
return i;
}
and called like
size_t numsize;
//...
numsize = store( arg, sizeof( arg ) / sizeof( *arg ), in_file );
Correspondingly the function printarray
should be defined like
void printarray( int arg[], size_t numsize, ostream& outfile)
{
for ( size_t i = 0; i < numsize; i++ )
^^^^^^^^^^^
{
outfile << arg[i] << endl;
}
}
Upvotes: 3
Reputation: 4689
There are at least two ways to fix it:
1) If you don't want to allow the function store()
to change it's second argument, just replace this line:
store(arg, numsize, in_file);
by this line:
numsize = store(arg, numsize, in_file);
2) Also you can just replace this line
int store (int arg[20], int numsize, istream& infile)
by this line
int store (int arg[20], int& numsize, istream& infile)
(in this case your function store()
will be able to change the value numsize
)
Also I'd recommend you to change the function store()
this way:
int store (int arg[20], istream& infile)
{
int numsize = 0;
while((numsize<20) && (infile >> arg[numsize]))
{
numsize++;
cout << numsize;
}
return numsize;
}
And to call it this way:
numsize = store(arg, in_file);
Upvotes: 3
Reputation: 726809
Since numsize
is passed by value, any modifications to it inside store
are discarded upon exiting. You need to assign the return value back to numsize
:
numsize = store(arg, numsize, in_file);
Note: You use numsize
in your store
function as if it were a local variable, because you assign it zero right away. Do not pass it at all:
int store (int arg[20], istream& infile) {
int numsize = 0;
while(numsize < 20 && (infile >> arg[numsize])) {
numsize++;
cout << numsize;
}
return numsize;
}
Also do not use infile.eof()
, this is an incorrect usage pattern
Upvotes: 4
Reputation: 2788
You need to pass numsize
by reference in store()
.
Change
int store (int arg[20], int numsize, istream& infile)
to
int store (int arg[20], int& numsize, istream& infile)
Also, either change store()
return type to void
, or return an int from it (i.e. numsize
?)
Upvotes: 3