0

I am calculating the largest value in the second column of all my files and then finding their proportional effect. My code works properly and gives me the correct values. However, num_voltages is not defined in the domain and so I'm confused about why this code works. I'm a complete beginner, I wrote this code with trial and error. When I replace 'num_voltages' with 'i', I get the wrong values. Anytime I try changing what I have currently written, it gives me the wrong values. There are 6 files with 2 columns. Does anyone have any suggestions on how to improve my code and solve the domain problem?

#include <fstream>
#include <sstream>
#include <iostream>
#include <cstring>
using namespace std;

int
main ()
{
  ifstream infile;
  ofstream outfile;
  int num_voltages;
  int num_concs;
  float peak_current;
  string filename;
  int conc;
  string conc_string;
  int i;

  i = 0;

  num_concs = 6;
  num_voltages = 16;
  peak_current = -10;
  float current[num_voltages];
  float voltage[num_voltages];

  outfile.open ("Conc_vs_peak_current.txt");

  for (conc = 0; conc <= 10; conc += 2)

    {
      filename = "IK_IV_" + to_string (conc) + ".txt";

      infile.open (filename.c_str ());

      cout << "The System is Reading " << filename << endl;

      if (!infile)
    {
      cout << "File Not Found. Exiting" << endl;
      exit (1);
    }

      for (i = 0; i < 16; i++)
    {
      infile >> voltage[num_voltages] >> current[num_voltages];
    }

      infile.close ();

      cout << "File Read" << endl;

      for (i = 0; i < 16; i++)
    {
      if (current[i] > peak_current)
        peak_current = current[num_voltages];
    }

      cout << "Peak Value = " << peak_current << ", " << "Voltage = " <<
    voltage[num_voltages] << endl;

      outfile << conc << " " << peak_current << " " << voltage[i] << endl;

      cout << endl << endl;

    }

  outfile.close ();


  ///////////////////////////////

  float current0[15];
  float voltage0[15];
  float current10[15];
  float voltage10[15];

  infile.open ("IK_IV_0.txt");

  for (i = 0; i < 16; i++)
    {
      infile >> voltage[num_voltages] >> current[num_voltages];
    }

  infile.close ();

  for (i = 0; i < 16; i++)
    {
      if (current[i] > peak_current)
    peak_current = current[num_voltages];
      current0[15] = peak_current;
    }


  infile.open ("IK_IV_10.txt");

  for (i = 0; i < 16; i++)
    {
      infile >> voltage[num_voltages] >> current[num_voltages];
    }

  infile.close ();

  for (i = 0; i < 16; i++)
    {
      if (current[i] > peak_current)
    peak_current = current[num_voltages];
      current10[15] = peak_current;
    }


  cout << endl << endl;

  /////////////////////////////


  outfile.open ("Proportional_effect.txt");

  for (conc = 0; conc <= 10; conc += 2)

    {
      filename = "IK_IV_" + to_string (conc) + ".txt";

      infile.open (filename.c_str ());

      cout << "The System is Reading " << filename << endl;

      if (!infile)
    {
      cout << "File Not Found. Exiting" << endl;
      exit (1);
    }

      for (i = 0; i < 16; i++)
    {
      infile >> voltage[num_voltages] >> current[num_voltages];
    }

      infile.close ();

      cout << "File Read" << endl;

      for (i = 0; i < 16; i++)
    {
      if (current[i] > peak_current)
        peak_current = current[num_voltages];
    }

      float numerator = peak_current - current0[15];
      float denominator = current10[15] - current0[15];
      float value = numerator / denominator;
      float absolute_value = abs (value);

      cout << "The Proportional Effect is " << absolute_value << endl;

      outfile << conc << " " << absolute_value << endl;

      cout << endl << endl;

    }

  outfile.close ();

}
Sam
  • 1
  • 4
    Its undefined behavior to attempt to access beyond the end of an array. The language says your program is broken and anything can happen including the worst behavior of the code appearing to work instead of crashing when it is broken. – drescherjm Feb 27 '23 at 01:56
  • Related: [https://stackoverflow.com/questions/367633/what-are-all-the-common-undefined-behaviours-that-a-c-programmer-should-know-a](https://stackoverflow.com/questions/367633/what-are-all-the-common-undefined-behaviours-that-a-c-programmer-should-know-a) – drescherjm Feb 27 '23 at 01:59
  • `float current[num_voltages];` is a *variable-length array*, and [C++ doesn't have those](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) – Some programmer dude Feb 27 '23 at 02:01
  • You also have the arrays `voltage`, `voltage0` and `voltage10`. Without any kind of documentation it's kind of confusing. And you read into `voltage` multiple times from multiple files. Is that by purpose? – Some programmer dude Feb 27 '23 at 02:03
  • 1
    As for your problem, you have `infile >> voltage[num_voltages] ...` Think about that for a while. – Some programmer dude Feb 27 '23 at 02:03

0 Answers0