How to create a C++ program for class average?

To I'm trying to get an output to look like this:

Enter the name of the student file: 
First      MI   Last               Average

Scott     A     Patterson      70.00
Jim         J     Jones            76.31
Ann       M     Smith            NO DATA FILE
Bobby    B     Baylor           NO GRADES
Krissy     B    Lastovica     75.17
Ronnie   R     Hinze           75.66
Richard  W    Gilpatrick     NO DATA FILE
Jeff         K    Pustka         75.02


Max Grade = 193.00
Min Grade = -53.00

Class average = 75.25

________________________________________________________________________________

Here's my C++ 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
  
  #include <iostream>
  #include <string>
  #include <fstream>
  #include <iomanip>
  #include <cstdlib>

   using namespace std;

   int main()
   {
      //Data Abstraction
       string lName, fName, mInit;
       string fileName;
       double sum = 0;
       double classAverage = 0;
       ifstream nameData;
       ifstream stData;
       int count = 0;
       bool firstData = true;
       double minGrade, maxGrade, Grade;
       double sumClass = 0, countClass = 0;

        cout << "Enter the name of the student file:" << endl;
        cout << "First" << "     " << "MI" << " " << "Last" << "       " << "Average" << endl;
        cout << endl;

        fileName = fName + lName + ".dat";
        cin >> fileName;

        nameData.open(fileName.c_str());

        if(!nameData){
            cout << "Error: Name File not Open.";
            exit (1);
        }

        while (nameData >> lName >> fName >> mInit)
        {
            stData.clear();
            stData.open(fileName.c_str());
            sum = 0;
            count = 0;

            //Do Some Work
            if(!stData){
              cout << "NO DATA FILE" << endl;
            }
            else
            {
              while (stData >> Grade)
                {
                 if(firstData)
                    {
                       minGrade = maxGrade = Grade;
                       firstData = false;
                    }
                if (minGrade > Grade)
                   {
                       minGrade = Grade;
                   }

                if (maxGrade < Grade)
                   {
                       maxGrade = Grade;
                   }


                }
                    count++;
                    sum += Grade;
                    countClass ++;
                    sumClass += Grade;

            }
            stData.close();

            if(count == 0)
               cout << "NO GRADES" << endl;
            else
               cout << sum/count << endl;
        }
            classAverage = sumClass/countClass;
            cout << endl;
            cout << endl;
            cout << "Max Grade =" << maxGrade << endl;
            cout << "Min Grade =" << minGrade << endl;
            cout << endl;
            cout << "Class Average =" << classAverage << endl;

      return 0;
  }

________________________________________________________________________________

But when I test it, I keep getting this:

Enter the name of the student file:
First     MI Last       Average

4.00209e-311
4.00209e-311
4.00209e-311
4.00209e-311
4.00209e-311
4.00209e-311
4.00209e-311
4.00209e-311


Max Grade =1.58101e-322
Min Grade =5.5766e-311

Class Average =4.00209e-311

_______________________________________________________________________________

Please tell me how to correct my coding so that I can get the output that I wanted. Thank you!
Last edited on
Please edit your post and format you code so we can read it. Look for the <> symbol to the right of the edit box, in the little Format: block.
1
2
3
4
5
6
7
8
9
fileName = fName + lName + ".dat";
cin >> fileName; //you just lost the previous value...

nameData.open(fileName.c_str());

while (nameData >> lName >> fName >> mInit)
{
  stData.clear();
  stData.open(fileName.c_str()); //the same as nameData 
Both streams point to the same file, one tries to read strings and the other numbers. That can't be right.
Last edited on
Ok, so I tried to re-do. My code was

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
99
#include <iostream>
  #include <string>
  #include <fstream>
  #include <iomanip>
  #include <cstdlib>

   using namespace std;

   int main()
   {
      //Data Abstraction
       string lName, fName, mInit;
       string fileName;
       double sum = 0.00;
       double classAverage = 0.00;
       ifstream nameData;
       ifstream stData;
       int count = 0;
       bool firstData = true;
       double minGrade, maxGrade, Grade;
       double sumClass = 0.00;
       double countClass = 0.00;

        cout << "Enter the name of the student file:" << endl;
        cout << "First" << "     " << "MI" << " " << "Last" << "       " << "Average" << endl;
        cout << endl;
        cin >> fileName;
        cout << fileName << endl;

        nameData.open(fileName.c_str());

        if(!nameData){
            cout << "Error: Name File not Open.";
            exit (1);
        }

        while (nameData >> lName >> fName >> mInit)
        {
            fileName = fName + lName + ".dat";

            stData.open(fileName.c_str());
            stData.clear();
            sum = 0.00;
            count = 0;
            sumClass = 0.00;
            countClass = 0;

            //Do Some Work
            if(!stData){
              cout << "NO DATA FILE" << endl;
            }
            else
            {
              while (stData >> Grade)
                {
                 if(firstData)
                    {
                       minGrade = maxGrade = Grade;
                       firstData = false;
                    }
                if (minGrade > Grade)
                   {
                       minGrade = Grade;
                   }

                if (maxGrade < Grade)
                   {
                       maxGrade = Grade;
                   }

                }
                    count++;
                    sum += Grade;
                    countClass ++;
                    sumClass += Grade;

            }
            stData.close();

            if(count == 0)
            {
                cout << "NO GRADES" << endl;
            }
            else
            {
               cout << sum/count << endl;
            }

        }
            classAverage = sumClass/countClass;
            cout << endl;
            cout << endl;
            cout << "Max Grade =" << maxGrade << endl;
            cout << "Min Grade =" << minGrade << endl;
            cout << endl;
            cout << "Class Average =" << classAverage << endl;

      return 0;
  }


My output turned out to be like:
Enter the name of the student file:
First     MI Last       Average

students.dat
193
79
79
79
86
94
94
99


Max Grade =193
Min Grade =-53

Class Average =99


Please tell me how to change my code to get it to work.
Topic archived. No new replies allowed.