title: Having trouble with calculations with a data file sort
body: <p>I am new to the stackoverflow communiy and new to coding so apologies in advance whie I learn the ropes of posting here as well as the rules of coding. I am using C++ and am in a CS161 beginner Computer Science class. </p>
  
  <p>I am currently working on an assignment which asks me to read from a data file stored on my computer and sort the data in order to do some calculations, which, in this assignment is finding  the average test scores based on sex and type of school. everything compiles and the program runs but there are a few problems. </p>
  
  <p>The first problem is with my echo. </p>
  
  <pre><code>    // echo the data file
      while (inData)
      {
      inData &gt;&gt; name &gt;&gt; sex &gt;&gt; school &gt;&gt; score;
      cout &lt;&lt; name &lt;&lt; sex &lt;&lt; school &lt;&lt; score &lt;&lt; endl;
  </code></pre>
  
  <p>The program does echo the data but it ends up echoing the last name on the list, twice for some reason. Also, (and I don't know if this matters) when it does echo, it does not skip spaces inbetween name, sex, school, and score.</p>
  
  <p>The second problem is that it isn't executing the calculations and I think it is because I am missing a "count" related instruction of some sort but I am stumped as far as what I can do.</p>
  
  <p>Here is my code, let me know what you think:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  #include &lt;iomanip&gt;
  using namespace std;
  int main()
  {
      //Declare variables to manipulate data
      char sex;
      string name;
      string school;
      string fileSource;
      string CC;
      string UN;
      int maleScore = 0;
      int femScore = 0;
      int unScore = 0;
      int ccScore = 0;
      double maleAvg;
      double femAvg;
      double unAvg;
      double ccAvg;
      double sumAvg = 0;
      int femCount = 0;
      int maleCount = 0;
      int ccCount = 0;
      int unCount = 0;
      int score;
      int sum;
      //Declare stream variables
      ifstream inData;
      ofstream outData;
      inData &gt;&gt; name &gt;&gt; sex &gt;&gt; school &gt;&gt; score;
      // Promt user for file location
      cout &lt;&lt; "Please input file location: ";
      cin &gt;&gt; fileSource;
  
      // open output file and run program exit failsafe command
      inData.open(fileSource);
      if (!inData)
      {
          cout &lt;&lt; "Cannot open input file. "
              &lt;&lt; "Program will now terminate." &lt;&lt; endl;
          return 1;
      }
      outData &lt;&lt; fixed &lt;&lt; showpoint &lt;&lt; setprecision(2);
  
      // echo the data file
      while (inData)
      {
          inData &gt;&gt; name &gt;&gt; sex &gt;&gt; school &gt;&gt; score;
          cout &lt;&lt; name &lt;&lt; sex &lt;&lt; school &lt;&lt; score &lt;&lt; endl;
  
          // while reading incoming data from file, execute the conditions
  
          // Male and female calculations
          if(sex=='M')
          {
              maleScore = maleScore +=score;
              ++maleCount;
          }
          else if(sex =='F')
          {
              femScore = femScore +=score;
              ++femCount;
          }
  
          // Community college and University calculations
          if(school == CC)
          {
              ccScore = ccScore +=score;
              ++ccCount;
          }
          else if(school == UN)
          {
              unScore = unScore +=score;
              ++unCount;
          }
          maleAvg = maleScore/maleCount;
      }
  
      // Male average output
      cout &lt;&lt; maleAvg;
  
      femAvg = femScore/femCount;
  
      // Female average output
      cout &lt;&lt; femAvg;
  
      ccAvg = ccScore/ccCount;
  
      // Community College average output
      cout &lt;&lt; ccAvg;
  
      unAvg = unScore/unCount;
  
      // University average output
      cout &lt;&lt; unAvg;
      sum = maleScore + femScore + ccScore + unScore;
      sumAvg = sum/12;
      cout &lt;&lt; sumAvg;
      return 0;
  }
  </code></pre>
  
  <p>Also, my compiler keeps running the program and does not stop. I took a pic of my compiler window but don't know how to post it.</p>