Help with functions! Won't display input??

I am having a really hard time with this program(I am new to programming). I have tried for several hours trying to figure out why my program will not display the input info.

Here is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cmath>

using namespace std;

const int ID_LOW = 200;
const int ID_HIGH = 299;

//Prototypes
void printHeading(ofstream& outFile);
void Rd(ifstream& inFile, string& studName, int& studId, float& totalGPA);
void printData(ofstream& outFile, string studName, int studId, float GPA);
void calcMax(float& maxOfGPA, float totalGPA);
float avg(float sum, int numOfRecords);
void printStatistic(ofstream& outFile, float maxOfGPA, float avgOfGPA);

int main()
{
  ifstream inFile;
  ofstream outFile;

  inFile.open("in.data");
  outFile.open("out.data");
  outFile.setf(ios::fixed);
  outFile.precision(2);

  string studName;
  int studId;
  int numOfRecords = 0;
  float totalGPA = 0.0;
  float maxOfGPA = 0.0;
  float avgOfGPA = 0.0;
  float sum = 0.0;

  printHeading(outFile);

  Rd(inFile, studName, studId, totalGPA);
  while(inFile)
    {
      if((studId >= ID_LOW) && (studId <= ID_HIGH))
        {
          printData(outFile, studName, studId, totalGPA);
          sum = sum + totalGPA;

          calcMax(maxOfGPA, totalGPA);

          numOfRecords++;
          avgOfGPA = avg(sum, numOfRecords);
    } //end While loop

          inFile >> studName >> studId >> totalGPA;
        }

        printStatistic(outFile, maxOfGPA, avgOfGPA);

        outFile << endl << "<* end *>" << endl;

  inFile.close();
  outFile.close();

  return 0;
}

void printHeading(ofstream& outFile)
{
  outFile << setw(10) << left << "Name" << setw(7) << left << "Id" << setw(7) << left << "GPA" << endl;
  outFile << setw(10) << left << "----" << setw(7) << left << "--" << setw(7) << left << "---" << endl;
}

void Rd(ifstream& inFile, string& studName, int& studId, float& totalGPA)
{
  inFile >> studName >> studId >> totalGPA;
}

void printData(ofstream& outFile, string studName, int studId, float GPA)
{
  outFile << setw(10) << left << studName << setw(7) << left << studId << setw(7) << left << GPA << endl;
}

void calcMax(float& maxOfGPA, float totalGPA)
{
  if(totalGPA > maxOfGPA)
    maxOfGPA = totalGPA;
}

float avg(float sum, int numOfRecords)
{
  return(sum/numOfRecords);
}

void printStatistic(ofstream& outFile, float maxOfGPA, float avgOfGPA)
{
  outFile << endl << "MAX GPA: " << maxOfGPA << endl;
  outFile << "AVG Of GPA: " << avgOfGPA << endl;
}

Last edited on
I think it might be where I'm declaring the function:
1
2
3
4
5
  while(inFile)
    {
      if(studId >= ID_LOW && studId <= ID_HIGH)
        {
          printData(outFile, studName, studId, totalGPA)


but when I look at the function itself it seems correct... I have no clue what is going on :(
closed account (3CXz8vqX)
I see lots of << and >>'s...but.... where are you supposed to be printing? I see no calls to cout or equivalent.

Just for syntax and to make sure that the program does exactly what you want...

if((studId >= ID_Low) && (... you get the picture
Last edited on
the "outFile's" are printing. All of my printing are under return 0;
Last edited on
illusion1158 wrote:
All of my printing are under return 0;
You mean like this?
1
2
3
4
5
6
7
#include <iostream>

int main()
{
    return 0;
    std::cout << "Hello, World!" << std::endl;
}
Topic archived. No new replies allowed.