Problems reading multiple numbers from a file using a function

I am attempting to read in multiple numbers from a file named testnum.txt (3 test grades for each student, 2 students for this example). Another function should then calculate the average for each student based on the 3 grades. Another function is called to determine if they receive a P or an F. The problem
is that the same numbers kept getting read in. I am still a beginner so any tips
are appreciated. Thank you.


What the input file looks like.
89 65 
92 67 
80 74


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
#include <fstream>
#include <iostream>
using namespace std;

ifstream infile;
ofstream outfile;
void grades(int&,int&,int&); // Grades from the input file
int myavg(int,int,int); // Average of the 3 grades
void letters(int&); // Letter Grade: (P)ass or (F)ail




int main()
{
    outfile.open("Table.txt");
    int x,y,z,average,stuid,letter;
    
    outfile << "\t\t\t\t\tStudent Information" << endl << endl;
    outfile << "Student ID\t\tGrade\t\tAverage\t\tLetter Grade" << endl<< endl;
    
    
    
    cout << "Please enter the Student's ID" << endl;
    cin >> stuid;
    
    
    while (stuid >= 0) {
          cout << "Please enter another Student's ID" << endl;
          cin >> stuid;
          grades(x,y,z);
          letters(x);
          outfile << "\t" << stuid << "\t\t\t\t" << myavg(x,y,z) << letters << 
          endl; 
          outfile << "\t\t\t" << x << endl;
          outfile << "\t\t\t" << y << endl;
          outfile << "\t\t\t" << z << endl;
          
   
}    

system("PAUSE");
return 0;
}
void grades(int &a,int &b,int &c)// Grades from the input file
{
     int x,y,z,;

    infile.open("testnum.txt");
    infile >> x,y,z; 
    

    return;
    
    
}
int myavg(int a, int b, int c)// Average of the 3 grades
{
    int x,y,z,average;
    
    average = (x + y + z) / 3;
    
    return average;
}
void letters(int& a)// Letter Grade: (P)ass or (F)ail
{
     int average;
     if (average >= 70)
         cout << "P" << endl;
     else
         cout << "F" << endl;
     return;
}
    
add this line on line 51 and you'll see it's not reading all your values.

cout << x << " " << y << " " << z << endl;

Try

infile >> x >> y >> z;
The first row is reading in correctly but that's all. I read somewhere that a loop could be used to read in data?
Last edited on
I made some adjustments but it still isn't reading the second line of my file.
I also changed the file being read.
1
2
90 83 94
72 65 81


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
#include <fstream>
#include <iostream>
using namespace std;

ifstream infile;
ofstream outfile;
void grades(int&,int&,int&); // Grades from the input file
int myavg(int,int,int); // Average of the 3 grades
void letters(int&); // Letter Grade: (P)ass or (F)ail
void heading(int); // Heading of Table




int main()
{
    
    int x,y,z,average,studentid,letter;
    
    heading(x);

    cout << "Please enter the Student's ID" << endl;
    cin >> studentid;
    
    
    while (studentid > 0) {
          
          letters(x);
          grades(x,y,z);
          outfile << "\t\t\t" << x << endl;
          outfile << "\t\t\t" << y << endl;
          outfile << "\t\t\t" << z << endl;
          outfile << "\t" << studentid << "\t\t\t\t" << myavg(x,y,z) << endl; 
          cout << "Please enter another Student's ID" << endl;
          cin >> studentid;
   
}    

system("PAUSE");
return 0;
}
void grades(int &a,int &b,int &c)// Grades from the input file
{
     int x,y,z,;

    infile.open("grades.txt");
    infile >> x >> y >> z; 
    cout << x << " " << y << " " << z << endl;

    return;
    
    
}
int myavg(int a, int b, int c)// Average of the 3 grades
{
    int x,y,z,average;
    
    average = (x + y + z) / 3;
    
    return average;
}
void letters(int& a)// Letter Grade: (P)ass or (F)ail
{
     int average;
     outfile.open("Table");
     if (average >= 70)
         outfile << "P" << endl;
     else
         outfile << "F" << endl;
     return;
}
void heading(int x)
{
     outfile.open("Table.txt");
     outfile << "\t\t\t\t\tStudent Information" << endl << endl;
     outfile << "Student ID\t\tGrade\t\tAverage\t\tLetter Grade" << endl<< endl;    
     return;
}




What the output looks like.
Please enter the Student's ID
1
90 83 94
Please enter another Student's ID
2
0 0 5461
Please enter another Student's ID
-1
Press any key to continue . . .
Topic archived. No new replies allowed.