File Reading and Sorting

Nov 1, 2018 at 4:34pm
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?


#include "pch.h"
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>

using namespace std;
string a, b, c;

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

cout << "Max Volt: " << maxv << endl;
cout << "Max Time: " << maxt << endl;
cout << "Max Amp: " << maxa << endl;

cout << "Min Volt: " << minv << endl;
cout << "Min Time: " << mint << endl;
cout << "Min Amp: " << mina << endl;
inFile.close();
}
}
Nov 1, 2018 at 5:11pm
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).


Nov 1, 2018 at 5:30pm
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. :(

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// ParsonsTabithaProject3.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include "pch.h"
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>

using namespace std;

int main(void)
{
	//Declare Variables
	string a, b, c;

	double time, volt, ampere;
	double maxVolt = 0, maxTime, maxApmere;
	double minVolt = 10, minTime, minAmpere;

	//Read from the Volts.txt file
	ifstream myFile("D:\\tabit\\Documents\\Volts.txt");

	//Check if the file can be opened
	if (myFile.is_open())
	{
		while (myFile >> a >> b >> c)
		{
			time = atof(a.c_str());
			volt = atof(b.c_str());
			ampere = atof(c.c_str());

			if (volt >= maxVolt)
			{
				maxTime = time;
				maxVolt = volt;
				maxApmere = ampere;
			}
			if (volt < minVolt)
			{
				minTime = time;
				minVolt = volt;
				minAmpere = ampere;
			}
		}
		//Close the file
		myFile.close();
	}
	//Give error message if the file cannot be opened
	else cout << "Unable to open file." << endl;

	//Display the Maximum results
	cout << "Max Volt: " << maxVolt << endl;
	cout << "Max Time: " << maxTime << endl;
	cout << "Max Ampere: " << maxApmere << endl;

	//Display the Minimum results
	cout << "Min Volt: " << minVolt << endl;
	cout << "Min Time: " << minTime << endl;
	cout << "Min Ampere: " << minAmpere << endl;

	return 0;
}
Nov 1, 2018 at 5:41pm
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)
		{
...
Nov 1, 2018 at 5:45pm
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.

Last edited on Nov 1, 2018 at 5:46pm
Nov 1, 2018 at 5:52pm
Now I am getting a Run Time error: "Run-Time Check Failure #3 - The variable 'maxTime' is being used without being initialized."

Yes, they are being read.

Sample of text file:
Time Volt Ampere
0.0001 9.77667 0.147408
0.00015 9.76583 0.147525
0.0002 9.76833 0.147692
0.00025 9.75833 0.147442
0.0003 9.76833 0.147192
0.00035 9.78167 0.1473
0.0004 9.76667 0.147317
0.00045 9.765 0.14715
0.0005 9.75667 0.147
0.00055 9.765 0.14695
0.0006 9.77 0.1471
0.00065 9.7675 0.147417
0.0007 9.7725 0.147417
0.00075 9.755 0.14735
0.0008 9.765 0.147725
0.00085 9.76583 0.147783

The min values should be
Time = 0.00075 Volt = 9.755 Ampere = 0.14735

The max values should be
Time= 0.00035 Volt = 9.78167 Ampere = 0.1473

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// ParsonsTabithaProject3.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include "pch.h"
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>

using namespace std;

int main(void)
{
	//Declare Variables
	string a, b, c;

	double time, volt, ampere;
	double maxVolt = 0, maxTime, maxApmere;
	double minVolt = 10, minTime, minAmpere;

	//Read from the Volts.txt file
	ifstream myFile("D:\\tabit\\Documents\\Volts.txt");

	//Check if the file can be opened
	if (myFile.is_open())
	{
		while (myFile >> time >> volt >> ampere)
		{
			if (volt >= maxVolt)
			{
				maxTime = time;
				maxVolt = volt;
				maxApmere = ampere;
			}
			if (volt < minVolt)
			{
				minTime = time;
				minVolt = volt;
				minAmpere = ampere;
			}
		}
		//Close the file
		myFile.close();
	}
	//Give error message if the file cannot be opened
	else return(1);

	//Display the Maximum results
	cout << "Max Volt: " << maxVolt << endl;
	cout << "Max Time: " << maxTime << endl;
	cout << "Max Ampere: " << maxApmere << endl;

	//Display the Minimum results
	cout << "Min Volt: " << minVolt << endl;
	cout << "Min Time: " << minTime << endl;
	cout << "Min Ampere: " << minAmpere << endl;

	return 0;
}
Last edited on Nov 1, 2018 at 6:08pm
Nov 1, 2018 at 6:10pm
Yes you have several variables that you should initialize when you define them.

1
2
	double maxVolt = 0, maxTime, maxApmere;
	double minVolt = 10, minTime, minAmpere;


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.


Nov 1, 2018 at 6:17pm
Are you saying that I need to initialize maxTime, maxAmpere, minTime, and minAmpere?
Nov 1, 2018 at 6:33pm
Yes you should always initialize variables before you try to use them in calculations.

Is that header line: "Time Volt Ampere" part of the input file? If so you need to "ignore" that line.

Nov 1, 2018 at 6:36pm
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.
Last edited on Nov 1, 2018 at 6:39pm
Nov 1, 2018 at 7:08pm
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.

Right before the while loop:
1
2
std::string temp;
getline(myFile, temp);


Topic archived. No new replies allowed.