I am trying to read a file with three columns and 1,000,000 rows of data and display the highest and lowest value along with its associated time and ampere. I am able to get it to work for the max value, but I keep getting zeros for the minimum values. Can you please help?
double time, volt, ampere;
double maxv = 0, maxt, maxa;
double minv = 10, mint, mina;
int main()
{
ifstream inFile;
inFile.open("D:\\tabit\\Documents\\Volts.txt");
if (inFile.fail())
{
cout << "Unable to open file.";
}
else
{
string s;
while (!inFile.eof())
{
inFile >> a >> b >> c;
time = atof(a.c_str());
volt = atof(b.c_str());
ampere = atof(c.c_str());
if (volt > maxv)
{
maxt = time;
maxv = volt;
maxa = ampere;
}
if (volt < minv)
{
mint = time;
minv = volt;
mina = ampere;
}
}
What's with all of those global variables? You only have one function so make those variables local to that function (defined inside main()).
Why are you using strings for input instead of just using the correctly typed variables (like volt, time, and ampere)? The extraction operator>> knows how to read the different standard types.
Also if you do stick with the strings you shouldn't be using atoi(), this C function can silently fail and give you bad data as a result. Instead use something like stod(), or stringstreams that either throw exceptions you can catch or cause the stream to fail which you can check.
You should also use the actual read operations to control your read loop instead of eof().
1 2
while (inFile >> a >> b >> c)
{
And lastly, for now, please use code tags when posting code (the <> icon to the right of the editor window).
I have cleaned up a bunch. I am unsure what you mean when you say, "Why are you using strings for input instead of just using the correctly typed variables (like volt, time, and ampere)?"
Also, I tried to use stod(), but I am unsure how to correctly use it. I did some research, but confused on how to apply it to my code.
The minimum values are still coming up as zeros. :(
Well start by getting rid of those three string variables and use the correct variables, volt, time, ampere instead.
Something more like:
1 2 3 4 5 6 7 8 9 10 11 12 13
//Check if the file can be opened
if (!myFile)
{
// Report the problem to the user.
// Stop the program.
return(1);
}
while (myFile >> time >> volt >> ampere)
{
if (volt >= maxVolt)
{
...
The minimum values are still coming up as zeros. :(
Did you verify that the values are all being read properly?
Please post a small sample of your input file.
What are the minimum and maximum values expected? Your min needs to be greater than your expected maximum value and your max should be less than the minimum expected value.
In the above only maxVolt and minVolt are initialized, all the variables should be initialized.
By the way why are you changing all the "maxes" based only on volt. I would expect that each "max" should be compared to the correct current reading. ie maxVolt to volt, maxTime to time, etc.
By the way what happens if volt is not less than minVolt and not greater than maxVolt? It looks to me like you need to work on your logic a bit.
Yes it is the first line in the text file, but I confused as to why I can find Max and not Min while it's not being ignored. This is just the first step for this program. The next step in my class is to do all of the exception stuff and enhancements. I really just need it to work for now.
Yes it is the first line in the text file, but I confused as to why I can find Max and not Min while it's not being ignored.
Because the first read operation failed because of the header. The failure caused the while loop not to execute leaving all the maxXX variables at their current states. Since maxVolts is initialized, but maxTime is not the second output statement failed. The rest of the program didn't execute because your system interrupted the rest of the program at that point. If you had initialized maxTime then the error would have been detected at maxAmpere, etc.
To solve this problem, before the loop read the first line into a "temporary" string and just discard (don't use) that string.