0

im having trouble with my program. it reads the student id, and both grades from a file and then a function its suppose to calculate the average of both grades and then pass the results to the main program using pass by reference. And for some reason my average its way off.
if anyone can help me out

currently my output looks like this:
Std-Id S1 S2 AVERAGE
-----------------------------------------------
126534 9 84.86621e-270
321345 7 34.86621e-270
324341 9 94.86621e-270
324341 9 94.86621e-270

this is what the output suppose to look like:

Std-Id S1 S2 AVERAGE
-----------------------------------------------
126534 9 8 8.5
321345 7 3 5.0
324341 9 9 9.0

#include <iostream>
#include <fstream>
#include <cmath>
#include <iomanip>
using namespace std;

double oneLine(double S1, double S2, double &avg);

int main()
{
  cout << "Std-Id" << setw(9) << "S1" << setw(6) << "S2" << setw(6)
       <<  "    AVERAGE" << endl;
  cout << "-----------------------------------------------" << endl;

  ifstream input("data4.txt");

  double student;
  double S1, S2;
  double avg;
  int line;

  if(!input)
    cout << "The input file doesn't exist" << endl;
  else
    while(!input.eof())
      {
        input >> student >> S1>> S2;
        oneLine(S1, S2, avg);
        cout << student << setw(9) << S1 << setw(6) << S2
             << setw(6) << avg << endl;

        line ++;
      }
  input.close();

  return 0;
}

double oneLine(double S1, double S2, double &avg)
  {
    double sum;
    double  result;
    sum = (S1 + S2)/2;
    return sum;
  }
2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by jonsca
0

while(!input.eof()) See this about eof.

Your function is passing in avg by reference but never doing anything with it. Either return your (S1+S2)/2 calculation and use it as such or make the function void and assign ave = (S1+S2)/2;

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.