Question: I am trying to write a C++ program that will read a txt file and file the highest and lowest volt and its associated time
I am trying to write a C++ program that will read a txt file and file the highest and lowest volt and its associated time and ampere. I can find the highest, but when I get to the lowest it just says 0. Can you please help?
#include "pch.h"
#include
#include
#include
#include
using namespace std;
string a, b, c;
double time, volt, ampere;
double maxVolt = 0, maxTime, maxApmere;
double minVolt = 10, minTime, minAmpere;
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 > maxVolt)
{
maxTime = time;
maxVolt = volt;
maxApmere = ampere;
}
if (volt < minVolt)
{
minTime = time;
minVolt = volt;
minAmpere = ampere;
}
}
cout << "Max Volt: " << maxVolt << endl;
cout << "Max Time: " << maxTime << endl;
cout << "Max Ampere: " << maxApmere << endl;
cout << "Min Volt: " << minVolt << endl;
cout << "Min Time: " << minTime << endl;
cout << "Min Ampere: " << minAmpere << endl;
inFile.close();
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
