Using a loop to read in data from a file?

My function is only reading the first line of numbers even though I have the loop
to keep returning to it in main(). How do I have it read from the file until it runs out of data to retrieve?

File being read
1
2
90 83 94
72 65 81


File being sent the data(Only the second average is correct, working on the first)
1
2
3
4
5
6
7
8
Student Information

Student ID		Grade		Average		Letter Grade

1					895612
			90 83 94
2					89
			90 83 94


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
#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); // Calculates the 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;
    //outfile.open("Table.txt");
    //heading(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;
    cout << "Please enter the Student's ID" << endl;
    cin >> studentid;
    
    
    while (studentid > 0) {
          
          
          
          
          outfile << studentid << "\t\t\t\t\t" << myavg(x,y,z) << endl;
          grades(x,y,z);
          outfile << "\t\t\t" << x << " " << y << " " << z << endl;
         // outfile << "\t\t\t" << y;
       //   outfile << "\t\t\t" << z; 
          
          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
{
     

    infile.open("grades.txt");
    infile >> a >> b >> c; 
    cout << a << " " << b << " " << c << endl;

   
    
    
}
int myavg(int a, int b, int c)// Average of the 3 grades
{
    int average;
    
    average = (a+ b+ c)/ 3;
    
    return average;
}
void letters(int& a)// Letter Grade: (P)ass or (F)ail
{
     int average;
     outfile.open("Table");
  //   if (average >= 70)
     //    cout << "P" << endl;
//     else
      //   cout << "F" << endl; 
     
}
open your input file in main and call your grades function before your myavg function
Last edited on
That actually did help me fix the averages so, thank you. But the the same row of numbers in my in file keep getting read.
In this function, would it be better to use eof? Also my loop only assigns to the grades to the first number that I cin for some reason.


1
2
3
4
5
6
7
8
9
10
11
void grades(int &a,int &b,int &c)// Grades from the input file
{
     int n,count=0;

    infile.open("grades.txt");
    while (infile >> n) {
          count++;
    
    infile >> a >> b >> c; 
    cout << "This is in the func " << a << " " << b << " " << c << endl;
}
Maybe use something like istream::getline(), and then parse the whole line? That'll solve your problem of only getting the first number of a line.

Ok I have since learned to read. If you're getting all the numbers of a single line only, yeah use while(!infile.eof()) or something like that.
Last edited on

The function I posted is now only reading the last line of my file >.>.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
Student Information

Student ID		Grade		Average		Letter Grade

			86 89 77
1					84
			86 89 77
2					84
			86 89 77
3					84
			86 89 77
4					84
			86 89 77
5					84





1
2
3
4
5
6
7
8
9
10
11
12
13
14
void grades(int &a,int &b,int &c)// Grades from the input file
{
    

    infile.open("grades.txt");
           
    while(!infile.eof()){
    
    infile >> a >> b >> c;
}
    return;
   
    
}


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
#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); // Calculates the 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,lettergrade;
    //outfile.open("Table.txt");
    //heading(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;
    cout << "Please enter the Student's ID" << endl;
    cin >> studentid;
    
    
    while (studentid > 0) {
          
          
          
          grades(x,y,z);
       
          outfile << "\t\t\t" << x << " " << y << " " << z << endl;
          outfile << studentid << "\t\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
{
    

    infile.open("grades.txt");
           
    while(!infile.eof()){
    
    infile >> a >> b >> c;
}
    return;
   
    
}   

int myavg(int a, int b, int c)// Average of the 3 grades
{
    int average;
    
    average = (a+ b+ c)/ 3;
    
    return average;
}
void letters(int& a)// Letter Grade: (P)ass or (F)ail
{
     int average;
     outfile.open("Table");
     if (average >= 70)
         cout << "P" << endl;
     else
         cout << "F" << endl; 
     
}

This function is reading all the numbers in the file however, it skips the first line and repeats the last one twice.
Please enter the Student's ID
1
72 65 81
74 99 52
48 70 67
86 89 77
86 89 77
Please enter another Student's ID
2
Please enter another Student's ID
-1
Press any key to continue . . .


1
2
3
4
5
6
7
8
9
10
11
12
13
void grades(int &a,int &b,int &c)// Grades from the input file
{
    

    infile.open("grades.txt");
    infile >> a >> b >> c;       
    while(infile){
    
    infile >> a >> b >> c;
    cout << a << " " << b << " " << c << endl;
}
    return;
   
I shuffled a few things around. Can anyone tell me why all the grades from the file are being applied to the same studentid that I cin?

The file with 5 sets of grades
1
2
3
4
5
90 83 94
76 56 99
87 79 100
23 10 43
71 62 44

1
90 83 94
76 56 99
87 79 100
23 10 43
71 62 44
71 62 44
50
2
50
3
50
4
50
5
50
-1
Press any key to continue . . .


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&,int&); // Grades from the input file
int myavg(int,int,int); // Calculates the Average of the 3 grades
void letters(int,int,int&); // Letter Grade: (P)ass or (F)ail
void heading(void); // Heading of the table




int main()
{
    
    int x,y,z,w,average,studentid,letter;
    
          heading();
          
          cin >> studentid; // Entering the student's ID
          while (studentid >= 0){
          grades(x,y,z,w);
          letters(average,x,y);
          average = myavg(y,z,w);
          outfile << "\t" <<  studentid << "\t\t\t" << y << " " << z << " " << w << "\t" 
          << " " << average << endl;
          cin >> studentid;
          }
         
                 
          
   

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

     infile.open("grades.txt");
           
    while(!infile.eof()){
    
    infile >> a >> b >> c;
    cout << a << " " << b << " " << c << endl;
}
    return;
}
int myavg(int a, int b, int c)// Average of the 3 grades
{
    int average;
    
    average = (a + b + c)/ 3;
    cout << average << endl;
    
    return average;
}
void letters(int a, int b, int& average)// Letter Grade: (P)ass or (F)ail
{
    
   
     if (average >= 70)
         outfile << "\t\t\t\t\t\t\t\t " << "P" << endl;
    else
         outfile << "\t\t\t\t\t\t\t\t " << "F" << endl; 
     
}
void heading(void) // Heading of the table
{
    outfile.open("Table.txt");
    outfile << "\t\t\t\t\tStudent Information" << endl << endl;
    outfile << "\tStudent ID\t\tGrade\t\tAverage\t\tLetter Grade" << endl<< endl;
    
    return;
}
Can anyone tell me why all the grades from the file are being applied to the same studentid that I cin?


Because the first time grades() is run you read the whole file, infile sets the eof flag and x,y,z take the values of the last line of grades. You print the average etc.

Next time you run grades(), infile's eof flag is still set (I don't think the open() changes that) a,b,c don't get printed to console and also x,y,z don't get changed so you print out same average as before.
Last edited on
Topic archived. No new replies allowed.