Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable returned from function reset to zero

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;
}
like image 907
Morgan Avatar asked Jun 10 '26 07:06

Morgan


2 Answers

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

like image 83
Sergey Kalinichenko Avatar answered Jun 12 '26 21:06

Sergey Kalinichenko


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);
like image 40
Ilya Avatar answered Jun 12 '26 23:06

Ilya



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!