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 ();
}