Reading Min and Max From Text File

The name of the pictureThe name of the pictureThe name of the pictureClash 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;










share|improve this question









New contributor




Tabitha Anne Daddis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.























    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;










    share|improve this question









    New contributor




    Tabitha Anne Daddis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





















      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;










      share|improve this question









      New contributor




      Tabitha Anne Daddis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      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






      share|improve this question









      New contributor




      Tabitha Anne Daddis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      Tabitha Anne Daddis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 5 hours ago









      AJNeufeld

      3,287317




      3,287317






      New contributor




      Tabitha Anne Daddis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 5 hours ago









      Tabitha Anne Daddis

      111




      111




      New contributor




      Tabitha Anne Daddis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Tabitha Anne Daddis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Tabitha Anne Daddis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















          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)





          share|improve this answer





























            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).






            share|improve this answer




















              Your Answer





              StackExchange.ifUsing("editor", function ()
              return StackExchange.using("mathjaxEditing", function ()
              StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
              StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
              );
              );
              , "mathjax-editing");

              StackExchange.ifUsing("editor", function ()
              StackExchange.using("externalEditor", function ()
              StackExchange.using("snippets", function ()
              StackExchange.snippets.init();
              );
              );
              , "code-snippets");

              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "196"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              convertImagesToLinks: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );






              Tabitha Anne Daddis is a new contributor. Be nice, and check out our Code of Conduct.









               

              draft saved


              draft discarded


















              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






























              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)





              share|improve this answer


























                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)





                share|improve this answer
























                  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)





                  share|improve this answer














                  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)






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 5 hours ago

























                  answered 5 hours ago









                  AJNeufeld

                  3,287317




                  3,287317






















                      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).






                      share|improve this answer
























                        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).






                        share|improve this answer






















                          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).






                          share|improve this answer












                          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).







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered 3 hours ago









                          Toby Speight

                          21k436103




                          21k436103




















                              Tabitha Anne Daddis is a new contributor. Be nice, and check out our Code of Conduct.









                               

                              draft saved


                              draft discarded


















                              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.













                               


                              draft saved


                              draft discarded














                              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













































































                              Comments

                              Popular posts from this blog

                              Long meetings (6-7 hours a day): Being “babysat” by supervisor

                              Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

                              Confectionery