Reading Min and Max From Text File
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
I am trying to write a program that will read the highest and lowest voltage along with its associated amp and time. I am able to find the max, but I am getting all zeros for the minimum value. I need help. Please and thank you.
Sample of txt 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
Expected Min Output:
Time = 0.00075 Volt = 9.755 Ampere = 0.14735
Expected Max Output:
Time= 0.00035 Volt = 9.78167 Ampere = 0.1473
int main(void)
//Declare Variables
string a, b, c;
double time, volt, ampere;
double maxVolt = 0, maxTime, maxAmpere;
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;
maxAmpere = 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: " << maxAmpere << endl;
//Display the Minimum results
cout << "Min Volt: " << minVolt << endl;
cout << "Min Time: " << minTime << endl;
cout << "Min Ampere: " << minAmpere << endl;
return 0;
c++ beginner
New contributor
add a comment |Â
up vote
2
down vote
favorite
I am trying to write a program that will read the highest and lowest voltage along with its associated amp and time. I am able to find the max, but I am getting all zeros for the minimum value. I need help. Please and thank you.
Sample of txt 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
Expected Min Output:
Time = 0.00075 Volt = 9.755 Ampere = 0.14735
Expected Max Output:
Time= 0.00035 Volt = 9.78167 Ampere = 0.1473
int main(void)
//Declare Variables
string a, b, c;
double time, volt, ampere;
double maxVolt = 0, maxTime, maxAmpere;
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;
maxAmpere = 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: " << maxAmpere << endl;
//Display the Minimum results
cout << "Min Volt: " << minVolt << endl;
cout << "Min Time: " << minTime << endl;
cout << "Min Ampere: " << minAmpere << endl;
return 0;
c++ beginner
New contributor
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I am trying to write a program that will read the highest and lowest voltage along with its associated amp and time. I am able to find the max, but I am getting all zeros for the minimum value. I need help. Please and thank you.
Sample of txt 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
Expected Min Output:
Time = 0.00075 Volt = 9.755 Ampere = 0.14735
Expected Max Output:
Time= 0.00035 Volt = 9.78167 Ampere = 0.1473
int main(void)
//Declare Variables
string a, b, c;
double time, volt, ampere;
double maxVolt = 0, maxTime, maxAmpere;
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;
maxAmpere = 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: " << maxAmpere << endl;
//Display the Minimum results
cout << "Min Volt: " << minVolt << endl;
cout << "Min Time: " << minTime << endl;
cout << "Min Ampere: " << minAmpere << endl;
return 0;
c++ beginner
New contributor
I am trying to write a program that will read the highest and lowest voltage along with its associated amp and time. I am able to find the max, but I am getting all zeros for the minimum value. I need help. Please and thank you.
Sample of txt 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
Expected Min Output:
Time = 0.00075 Volt = 9.755 Ampere = 0.14735
Expected Max Output:
Time= 0.00035 Volt = 9.78167 Ampere = 0.1473
int main(void)
//Declare Variables
string a, b, c;
double time, volt, ampere;
double maxVolt = 0, maxTime, maxAmpere;
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;
maxAmpere = 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: " << maxAmpere << endl;
//Display the Minimum results
cout << "Min Volt: " << minVolt << endl;
cout << "Min Time: " << minTime << endl;
cout << "Min Ampere: " << minAmpere << endl;
return 0;
c++ beginner
c++ beginner
New contributor
New contributor
edited 5 hours ago
AJNeufeld
3,287317
3,287317
New contributor
asked 5 hours ago
Tabitha Anne Daddis
111
111
New contributor
New contributor
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
2
down vote
The value of atof("Volt")
is zero. The corresponding values of atof("Time")
and atof("Ampere")
are also zero, which gives you the minimum values of zero you observed.
You need to skip the first line of the file. Adding this before the while loop would work:
myFile.ignore(80, 'n');
Use std::numeric_limits<std::streamsize>::max()
instead of 80
for a more correct (but verbose) limit of characters to skip before the new line character.
After you add the skipping of the first line, you no longer need the a, b & c variables and atof()
calls; you can read the values directly:
while (myFile >> time >> volt >> ampere)
add a comment |Â
up vote
2
down vote
Your code is missing the headers you need - at least <fstream>
and <iostream>
. It also seems that there's a using namespace std;
lurking somewhere; that's a bad practice that can subtly break your code, so I recommend that you explicitly qualify the names you use from it (std
is intentionally a very short name, to save typing).
In C++, we can declare int main()
- that's a prototype, unlike in C, where we need to write int main(void)
. The latter is certainly foreign-looking in C++.
It's probably clearer to return early if the file opening failed:
if (!myFile)
std::cerr << "Couldn't open input file";
return 1;
I'm a little uncomfortable with the compiled-in filename to read from - this makes the program quite inflexible. Either provide the name as a command-line argument, or simply read from standard input so the program can read from any file or from a pipeline.
There's little point closing the input stream if we ignore the result - the stream's destructor will do that for us, so just let it go out of scope.
Be warned that std::endl
includes a flush of the output stream - it's better to just write 'n'
as line terminator (the stream will be flushed at program exit).
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
The value of atof("Volt")
is zero. The corresponding values of atof("Time")
and atof("Ampere")
are also zero, which gives you the minimum values of zero you observed.
You need to skip the first line of the file. Adding this before the while loop would work:
myFile.ignore(80, 'n');
Use std::numeric_limits<std::streamsize>::max()
instead of 80
for a more correct (but verbose) limit of characters to skip before the new line character.
After you add the skipping of the first line, you no longer need the a, b & c variables and atof()
calls; you can read the values directly:
while (myFile >> time >> volt >> ampere)
add a comment |Â
up vote
2
down vote
The value of atof("Volt")
is zero. The corresponding values of atof("Time")
and atof("Ampere")
are also zero, which gives you the minimum values of zero you observed.
You need to skip the first line of the file. Adding this before the while loop would work:
myFile.ignore(80, 'n');
Use std::numeric_limits<std::streamsize>::max()
instead of 80
for a more correct (but verbose) limit of characters to skip before the new line character.
After you add the skipping of the first line, you no longer need the a, b & c variables and atof()
calls; you can read the values directly:
while (myFile >> time >> volt >> ampere)
add a comment |Â
up vote
2
down vote
up vote
2
down vote
The value of atof("Volt")
is zero. The corresponding values of atof("Time")
and atof("Ampere")
are also zero, which gives you the minimum values of zero you observed.
You need to skip the first line of the file. Adding this before the while loop would work:
myFile.ignore(80, 'n');
Use std::numeric_limits<std::streamsize>::max()
instead of 80
for a more correct (but verbose) limit of characters to skip before the new line character.
After you add the skipping of the first line, you no longer need the a, b & c variables and atof()
calls; you can read the values directly:
while (myFile >> time >> volt >> ampere)
The value of atof("Volt")
is zero. The corresponding values of atof("Time")
and atof("Ampere")
are also zero, which gives you the minimum values of zero you observed.
You need to skip the first line of the file. Adding this before the while loop would work:
myFile.ignore(80, 'n');
Use std::numeric_limits<std::streamsize>::max()
instead of 80
for a more correct (but verbose) limit of characters to skip before the new line character.
After you add the skipping of the first line, you no longer need the a, b & c variables and atof()
calls; you can read the values directly:
while (myFile >> time >> volt >> ampere)
edited 5 hours ago
answered 5 hours ago
AJNeufeld
3,287317
3,287317
add a comment |Â
add a comment |Â
up vote
2
down vote
Your code is missing the headers you need - at least <fstream>
and <iostream>
. It also seems that there's a using namespace std;
lurking somewhere; that's a bad practice that can subtly break your code, so I recommend that you explicitly qualify the names you use from it (std
is intentionally a very short name, to save typing).
In C++, we can declare int main()
- that's a prototype, unlike in C, where we need to write int main(void)
. The latter is certainly foreign-looking in C++.
It's probably clearer to return early if the file opening failed:
if (!myFile)
std::cerr << "Couldn't open input file";
return 1;
I'm a little uncomfortable with the compiled-in filename to read from - this makes the program quite inflexible. Either provide the name as a command-line argument, or simply read from standard input so the program can read from any file or from a pipeline.
There's little point closing the input stream if we ignore the result - the stream's destructor will do that for us, so just let it go out of scope.
Be warned that std::endl
includes a flush of the output stream - it's better to just write 'n'
as line terminator (the stream will be flushed at program exit).
add a comment |Â
up vote
2
down vote
Your code is missing the headers you need - at least <fstream>
and <iostream>
. It also seems that there's a using namespace std;
lurking somewhere; that's a bad practice that can subtly break your code, so I recommend that you explicitly qualify the names you use from it (std
is intentionally a very short name, to save typing).
In C++, we can declare int main()
- that's a prototype, unlike in C, where we need to write int main(void)
. The latter is certainly foreign-looking in C++.
It's probably clearer to return early if the file opening failed:
if (!myFile)
std::cerr << "Couldn't open input file";
return 1;
I'm a little uncomfortable with the compiled-in filename to read from - this makes the program quite inflexible. Either provide the name as a command-line argument, or simply read from standard input so the program can read from any file or from a pipeline.
There's little point closing the input stream if we ignore the result - the stream's destructor will do that for us, so just let it go out of scope.
Be warned that std::endl
includes a flush of the output stream - it's better to just write 'n'
as line terminator (the stream will be flushed at program exit).
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Your code is missing the headers you need - at least <fstream>
and <iostream>
. It also seems that there's a using namespace std;
lurking somewhere; that's a bad practice that can subtly break your code, so I recommend that you explicitly qualify the names you use from it (std
is intentionally a very short name, to save typing).
In C++, we can declare int main()
- that's a prototype, unlike in C, where we need to write int main(void)
. The latter is certainly foreign-looking in C++.
It's probably clearer to return early if the file opening failed:
if (!myFile)
std::cerr << "Couldn't open input file";
return 1;
I'm a little uncomfortable with the compiled-in filename to read from - this makes the program quite inflexible. Either provide the name as a command-line argument, or simply read from standard input so the program can read from any file or from a pipeline.
There's little point closing the input stream if we ignore the result - the stream's destructor will do that for us, so just let it go out of scope.
Be warned that std::endl
includes a flush of the output stream - it's better to just write 'n'
as line terminator (the stream will be flushed at program exit).
Your code is missing the headers you need - at least <fstream>
and <iostream>
. It also seems that there's a using namespace std;
lurking somewhere; that's a bad practice that can subtly break your code, so I recommend that you explicitly qualify the names you use from it (std
is intentionally a very short name, to save typing).
In C++, we can declare int main()
- that's a prototype, unlike in C, where we need to write int main(void)
. The latter is certainly foreign-looking in C++.
It's probably clearer to return early if the file opening failed:
if (!myFile)
std::cerr << "Couldn't open input file";
return 1;
I'm a little uncomfortable with the compiled-in filename to read from - this makes the program quite inflexible. Either provide the name as a command-line argument, or simply read from standard input so the program can read from any file or from a pipeline.
There's little point closing the input stream if we ignore the result - the stream's destructor will do that for us, so just let it go out of scope.
Be warned that std::endl
includes a flush of the output stream - it's better to just write 'n'
as line terminator (the stream will be flushed at program exit).
answered 3 hours ago
Toby Speight
21k436103
21k436103
add a comment |Â
add a comment |Â
Tabitha Anne Daddis is a new contributor. Be nice, and check out our Code of Conduct.
Tabitha Anne Daddis is a new contributor. Be nice, and check out our Code of Conduct.
Tabitha Anne Daddis is a new contributor. Be nice, and check out our Code of Conduct.
Tabitha Anne Daddis is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f206744%2freading-min-and-max-from-text-file%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password