Array in class and File operation

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
100
101
102
103
104
105
106
107
... the code goes here
#define SIZE 25

#include <iostream>
#include <fstream>
using namespace std;

class Student
{
	private:
		string studentName;
		string matrixNumber;
		char answer[SIZE];
		char grade;
		float marks;
		
	public:
		void getData()
		{
			ifstream infile("lab1txt.txt");
			int num = 0;
			
		 	if (infile.fail())
		   		cout << "Sorry, input file not exist!" << endl;
		   	else
		 	{
		   		while (!infile.eof( ))      //if not at end of file, continue reading input data
				{
					infile >> studentName >> matrixNumber;
					while (num<SIZE)
					{
						infile >> answer[num];
					}
				}
		 	}
			
			infile.close();
		}
		
		void gradeTest()	//check answer
		{
			int num=0;
			marks=0;
			
			char correct[] =  {"BACDACBDABBACADAABACABCDA"};
			
			while(num<SIZE)
			{
				if(answer[num] == correct[num])
				{
					marks=marks+4;
				}
				else
				{
					marks=marks-1;
				}
			}
			
			if(marks>=75 && marks<=100)
			{
				grade='A';
			}
			else if(marks>=65 && marks<=74)
			{
				grade='B';
			}
			else if(marks>=55 && marks<=64)
			{
				grade='C';
			}
			else if(marks>=45 && marks<=54)
			{
				grade='D';
			}
			else if(marks>=0 && marks<=44)
			{
				grade='E';
			}
		}
		
		void printDetail()
		{											
			cout << "Name\t" << "Matrix No\t\t" << "	 Student's answer	 " << "Grade"
				 << studentName << matrixNumber;
			
			for(int i=0; i<SIZE; i++)
			{
				cout << answer[i] << "\t";
			}
			
			cout << grade;
		}
};

int main()
{
	Student stud[3];
	
	for(int i=0; i<3; i++)
	{
		stud[i].getData();
		stud[i].gradeTest();
		stud[i].printDetail();
	}
	
	return 0;
}


The input file:
Name Matrix No Student’s answer
Hilmi 11111 ABCDABCDABCDABCDABCDABCDA
Farid 11122 BBCCDDAABBCCDDAABBCCDDAAB
Najwa 11133 AAAAABBBBBCCCCCDDDDDABCDA

I faced a problem when running the program, no syntax error and the console is blank, Please help me to find my mistakes, Thank you in advance!
Where is the Student constructor? Need a destructor too!
A couple of problems with getData().

Line 181: getData() is executed three times. Each time it reads the entire file leaving only the last record in memory.

Line 27: Do not loop on (! stream.eof()) or (stream.good()). This does not work the way you expect. The eof bit is set true only after you make a read attempt on the file. This means after you read the last record of the file, eof is still false. Your attempt to read past the last record sets eof, but you're not checking it there. You proceed as if you had read a good record. This will result in reading an extra (bad) record. The correct way to deal with this is to put the >> (or getline) operation as the condition in the while statement.
1
2
3
  while (stream >> var) // or while (getline(stream,var))
  {  //  Good operation
  }


Line 30: You never increment num so you're always reading into the same char.

As a start it might be an idea to separate file reading from the Student class.

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
#include <iostream>
#include <fstream>

using namespace std;

class Student
{
private:
    string studentName{"Unknown"};
    int matrixNumber{0};
    string answer{"???"};
    char grade{'?'};
    double marks{-99.9};
    
public:
    Student(){};
    Student(string aName, int aMatrix, string aAnswer)
    {
        studentName = aName;
        matrixNumber = aMatrix;
        answer = aAnswer;
    }
    
    void setName(string aName)
    {
        studentName = aName;
    }
    
    void setMatrixNo(int aMatrix)
    {
        matrixNumber = aMatrix;
    }
    
    void setAnswer(string aAnswer)
    {
        answer = aAnswer;
    }
    
    ~Student(){};
    
    void print()
    {
        cout
        << "Name: " << studentName
        << " Matrix No.: " << matrixNumber
        << " Student's answer: " << answer
        << " Grade: " << grade
        << '\n';
    }
};

const int LIMIT{20};

int main()
{
    Student stud[LIMIT];
    
    string heading;
    string name;
    int mat_no;
    string ans;
    
    int NO_STUDENTS{0};
    
    ifstream infile("lab1txt.txt");
    if (!infile)
    {
        cout << "Sorry, input file not exist!\n";
        return -999;
    }
    else
    {
        getline(infile, heading);
        
        while (infile >> name >> mat_no >> ans)
        {
            stud[NO_STUDENTS].setName(name);
            stud[NO_STUDENTS].setMatrixNo(mat_no);
            stud[NO_STUDENTS].setAnswer(ans);
            NO_STUDENTS++;
        }
        
        infile.close();
    }
    
    cout << heading << '\n';
    for(int i = 0; i < NO_STUDENTS; i++)
    {
        stud[i].print();
    }
    
    return 0;
}



Name Matrix No Student’s answer
Name: Hilmi Matrix No.: 11111 Student's answer: ABCDABCDABCDABCDABCDABCDA Grade: ?
Name: Farid Matrix No.: 11122 Student's answer: BBCCDDAABBCCDDAABBCCDDAAB Grade: ?
Name: Najwa Matrix No.: 11133 Student's answer: AAAAABBBBBCCCCCDDDDDABCDA Grade: ?
Program ended with exit code: 0
and the console is blank, Please help me to find my mistakes,


This when the debugger becomes your new friend. if you don't know how to use it, now is the time to learn it. All programmes need (must) know how to use it properly. Use the debugger to trace through the code and watch the contents of the variables and the path taken through the code.
Topic archived. No new replies allowed.