0

I started writing this code as practice for my Computer Science class. The goal was to help with structs and arrays. But I have come to a problem. The file will compile, but returns a message saying "floating point exception." Doing research I see there could a few reasons, but I can't stop any problems with my code. I was hoping if I had someone more experienced look at the code, maybe I can get help understanding what I did. BEWARE: I am just a beginner with C++. This may or may not hurt your eyes. Just some info on the program, it is suppose to open a file, read in the name and grade into a array, then calculate the average and find a curve. Then it is suppose to print out the new grades, with the curve added to it.

Here is the code:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

const int SIZE = 200;

struct Grades
{
   string student;
     int grade;
};

void LoadGrades(Grades [], int &);
int Curve(Grades [], int);

int main()
{
  int curve;
    int i = 0;
  Grades info[SIZE];

  LoadGrades(info, i);
  curve = Curve(info, i);
  for (int p = 0; p < i; p++)
{
  cout << info[p].student << endl;
  cout << info[p].grade + curve << endl;
}

  return 0;
}


void LoadGrades(Grades g[], int & i)
{

   ifstream inFile;
   string dud;
   Grades info;

   i = 0;
   if (!inFile) return;

   getline(inFile, info.student);
   inFile >> info.grade;

   while (inFile && i < SIZE)
  {
    g[i] = info;
    i++;

    getline(inFile, dud);
    getline(inFile, info.student);
    inFile >> info.grade;
   }
   inFile.close();
}

int Curve(Grades g[], int n)
{
  int avg, curve, i, data;
  int sum = 0;
  int count = 0;

  for (i = 0; i < n; i++)
{
  data = g[i].grade;
  sum = sum + data;
  count++;
}

  avg = sum / count;

  curve = 75 - avg;

   return curve;
}

Edited by Dani: Formatting fixed

2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by gerard4143
1

Take a look at this function call

curve = Curve(info, i);

Your passing i = 0 and what do you think will happen in this function when n = 0?

int Curve(Grades g[], int n)
{
int avg, curve, i, data;
int sum = 0;
int count = 0;

for (i = 0; i < n; i++)//n = 0
{
data = g[i].grade;
sum = sum + data;
count++;//never reach this
}

avg = sum / count;//what happens here? count = 0

curve = 75 - avg;

return curve;
}

Edited by gerard4143: n/a

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.