Getting a wrong output when running the program

Everything looks correct on this project, but the output is not correct.
Here is what I get as output:

Student Name: Lisa Miller
Student ID: 890238
Number of courses enrolled: 0

Course No Course Name Credits Grade

Total number of credit hours: 0
Midsemester GPA: nan
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

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
#ifndef personType_H
#define personType_H

#include <string>

using namespace std;

class personType
{
public:
	void print() const;
	  //Functin to output first
	  //name and last name in the
	  //form firstName lastName

	void setName(string first, string last);
	  //Function to set firstName and lastName according to
 	  //the parameters
	  //Postcondition: firstName = first; lastName = last;

	void getName(string& first, string& last);
	  //Function to return firstName and lastName via the parameters
	  //Postcondition:  first = firstName; last = lastName;

	personType(string first, string last);
	  //constructor with parameters
	  //set firstName and lastName according to the parameters
	  //Postcondition: firstName = first; lastName = last;

  	personType();
  	  //default constructor;
  	  //intialize firstName and lastName to empty string
  	  //Postcondition: firstName = ""; lastName = "";

private:
	string firstName; //store first name
	string lastName;  //store last name
};
#endif

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
//personType Implementation File

#include <iostream>
#include "personType.h"
#include <string>

using namespace std;

void personType::print() const
{
	cout<<firstName<<" "<<lastName;
}

void personType::setName(string first, string last)
{
	firstName = first;
	lastName = last;
}

void personType::getName(string& first, string& last)
{
	first = firstName;
	last = lastName;
}

//constructor with parameters
personType::personType(string first, string last)

{
	firstName = first;
	lastName = last;
}

personType::personType()   //default constructor
{
	firstName = "";
	lastName = "";
}

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
#ifndef H_studentType
#define H_studentType

#include <fstream>
#include <string>
#include <vector>

#include "personType.h"
#include "courseType.h"

using namespace std;

class studentType: public personType
{
public:
    void setInfo(string fname, string lName, int ID,
		         bool isTPaid,
    		     vector<courseType> courses);
		//Function to set the student's information
		//The private data members are set according
		//to the parameters.

    void print(double tuitionRate);
		//Function to print the student's grade report

    void print(ofstream& out, double tuitionRate);
		//Function to print the student's grade report
		//The output is stored in a file specified by the
		//parameter out.

    studentType();
		//Default constructor
		//Postcondition: Data members are initialized to
 		//the default values

    int getHoursEnrolled();
		//Function to return the credit hours a student
		//is enrolled in.
		//Postcondition: The number of credit hours in which a
		//   student is enrolled is calculated and returned

    double getGpa();
		//Function to return the grade point average.
		//Postcondition: The GPA is calcualted and returned.

    double billingAmount(double tuitionRate);
		//Function to return the tuition fees
		//Postcondition: The tuition fees due is calcualted
		//              and returned.

private:
    int sId;				//variable to store the student ID
    int numberOfCourses;	//variable to store the number
							//of courses
    bool isTuitionPaid;		//variable to indicate if the tuition
							//is paid
    vector<courseType> coursesEnrolled; //vector to store the courses
};

#endif 

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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <algorithm>
#include <vector>
#include <iterator>

#include "personType.h"
#include "courseType.h"
#include "studentType.h"

using namespace std;

void studentType::setInfo(string fName, string lName, int ID,
                          bool isTPaid,
                          vector<courseType> courses)
{
   personType::setName(fName,lName); 	//set the name

   sId = ID;							//set the student ID
   isTuitionPaid = isTPaid;				//set isTuitionPaid
   numberOfCourses = courses.size();	//set the number of courses

	coursesEnrolled = courses;  //set the vector coursesEnrolled

		//sort the array coursesEnrolled
	sort(coursesEnrolled.begin(), coursesEnrolled.end());
}

studentType::studentType()
{
	numberOfCourses = 0;
	sId = 0;
	isTuitionPaid = false;
}

void studentType::print(double tuitionRate)
{
	int i;

	cout<<"Student Name: ";							//Step 1
	personType::print();							//Step 1
	cout<<endl;

	cout<<"Student ID: "<<sId<<endl;				//Step 2

	cout<<"Number of courses enrolled: "
		<<numberOfCourses<<endl;					//Step 3
	cout<<endl;

	cout<<left;		//set output left-justified
	cout<<"Course No"<<setw(15)<<"  Course Name"
		<<setw(8)<<"Credits"
		<<setw(6)<<"Grade"<<endl;					//Step 4
	cout.unsetf(ios::left);

	for(i = 0; i < numberOfCourses; i++)			//Step 5
	    coursesEnrolled[i].print(isTuitionPaid);
	cout<<endl;

	cout<<"Total number of credit hours: "
		<<getHoursEnrolled()<<endl;					//Step 6

	cout<<fixed<<showpoint<<setprecision(2);		//Step 7

	if(isTuitionPaid)								//Step 8
	   cout<<"Midsemester GPA: "<<getGpa()<<endl;
	else
	{
	    cout<<"*** Grades are being held for not paying "
			<<"the tuition. ***"<<endl;
	    cout<<"Amount Due: $"<<billingAmount(tuitionRate)
    		<<endl;
	}

	cout<<"-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*"
          <<"-*-*-*-*-*-*-"<<endl<<endl;
}

void studentType::print(ofstream& outp, double tuitionRate)
{
	int i;
	string first;
	string last;

	personType::getName(first,last);

	outp<<"Student Name: "<<first<<" "<<last<<endl;

	outp<<"Student ID: "<<sId<<endl;

	outp<<"Number of courses enrolled: "
		<<numberOfCourses<<endl;
	outp<<endl;

	outp<<left;
	outp<<"Course No"<<setw(15)<<"  Course Name"
		<<setw(8)<<"Credits"
		<<setw(6)<<"Grade"<<endl;

	outp.unsetf(ios::left);

	for(i = 0; i < numberOfCourses; i++)
		coursesEnrolled[i].print(outp,isTuitionPaid);
	outp<<endl;

	outp<<"Total number of credit hours: "
		<<getHoursEnrolled()<<endl;

	outp<<fixed<<showpoint<<setprecision(2);

	if(isTuitionPaid)
	    outp<<"Midsemester GPA: "<<getGpa()<<endl;
	else
	{
	    outp<<"*** Grades are being held for not paying "
			<<"the tuition. ***"<<endl;
	    outp<<"Amount Due: $"<<billingAmount(tuitionRate)
  			<<endl;
	}

	outp<<"-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*"
          <<"-*-*-*-*-"<<endl<<endl;
}

int studentType::getHoursEnrolled()
{
	int totalCredits = 0;
	int i;

	for(i = 0; i < numberOfCourses; i++)
		totalCredits += coursesEnrolled[i].getCredits();
	return totalCredits;
}

double studentType::billingAmount(double tuitionRate)
{
	return tuitionRate * getHoursEnrolled();
}

double studentType::getGpa()
{
	int i;
	double sum = 0.0;

	for(i = 0; i < numberOfCourses; i++)
	{
		switch(coursesEnrolled[i].getGrade())
		{
		case 'A': sum += coursesEnrolled[i].getCredits() * 4;
				  break;
		case 'B': sum += coursesEnrolled[i].getCredits() * 3;
				  break;
		case 'C': sum += coursesEnrolled[i].getCredits() * 2;
				  break;
		case 'D': sum += coursesEnrolled[i].getCredits() * 1;
				  break;
		case 'F': sum += coursesEnrolled[i].getCredits() * 0;
				  break;
		default: cout<<"Invalid Course Grade"<<endl;
		}
	}

	return sum / getHoursEnrolled();
}

The continuation of the project:
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
#ifndef H_courseType
#define H_courseType
#include <fstream>
#include <string>
using namespace std;

class courseType
{
public:
    void setCourseInfo(string cName, string cNo,
  					   char grade, int credits);
		//Function to set the course information
  		//The course information is set according to the
		//incoming parameters.
		//Postcondition: courseName = cName; courseNo = cNo;
  		//     courseGrade = grade; courseCredits = credits;

    void print(bool isGrade);
		//Function to print the course information
  		//This function prints the course information on the
		//screen. Furthermore, if the bool parameter isGrade is
    	//true, the grade is shown, otherwise three stars
		//are printed.

    void print(ofstream& outp, bool isGrade);
		//Function to print the course information
  		//This function sends the course information to a file.
 		//Furthermore, if the bool parameter isGrade is true,
		//the grade is shown, otherwise three stars are printed.

    int getCredits();
		//Function to return the credit hours
		//The value of the private data member courseCredits
		//is returned.

    void getCourseNumber(string& cNo);
		//Function to return the course number
		//Postcondition:  cNo = courseNo;

    char getGrade();
		//Function to return the grade for the course
		//The value of the private data member courseGrade
		//is returned

	bool operator==(const courseType&) const;
	bool operator!=(const courseType&) const;
	bool operator<=(const courseType&) const;
	bool operator<(const courseType&) const;
	bool operator>=(const courseType&) const;
	bool operator>(const courseType&) const;

   courseType(string cName = "", string cNo = "",
			  char grade = '*', int credits = 0);
  		//Constructor
		//The object is initialized according to the
		//parameters.
		//Postcondition: courseName = cName; courseNo = cNo;
  		//     courseGrade = grade; courseCredits = credits;

private:
    string courseName; 	//variable to store the course name
    string courseNo; 	//variable to store the course number
    char courseGrade;	//variable to store the grade
    int courseCredits;	//variable to store the grade
};
#endif

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 "courseType.h"

using namespace std;

void courseType::setCourseInfo(string cName, string cNo,
   						       char grade, int credits)
{
	courseName = cName;
	courseNo = cNo;
	courseGrade = grade;
	courseCredits = credits;
}

void courseType::print(bool isGrade)
{
	cout<<left;								//Step 1
	cout<<setw(8)<<courseNo<<"   ";			//Step 2
	cout<<setw(15)<<courseName;				//Step 3
	cout.unsetf(ios::left);					//Step 4
	cout<<setw(3)<<courseCredits<<"   ";	//Step 5

	if(isGrade)								//Step 6
		cout<<setw(4)<<courseGrade<<endl;
	else
		cout<<setw(4)<<"***"<<endl;
}

void courseType::print(ofstream& outp, bool isGrade)
{
	outp<<left;								//Step 1
	outp<<setw(8)<<courseNo<<"   ";			//Step 2
	outp<<setw(15)<<courseName;				//Step 3
	outp.unsetf(ios::left);					//Step 4
	outp<<setw(3)<<courseCredits<<"   ";	//Step 5

	if(isGrade)								//Step 6
		outp<<setw(4)<<courseGrade<<endl;
	else
		outp<<setw(4)<<"***"<<endl;
}

courseType::courseType(string cName, string cNo,
					   char grade, int credits)
{
	courseName = cName;
	courseNo =  cNo;
	courseGrade = grade;
	courseCredits = credits;
}

int courseType::getCredits()
{
	return courseCredits;
}

char courseType::getGrade()
{
	return courseGrade;
}

void courseType::getCourseNumber(string& cNo)
{
	cNo = courseNo;
}

bool courseType::operator==(const courseType& right) const
{
	return (courseNo == right.courseNo);
}

bool courseType::operator!=(const courseType& right) const
{
	return (courseNo != right.courseNo);
}

bool courseType::operator<=(const courseType& right) const
{
	return (courseNo <= right.courseNo);
}

bool courseType::operator<(const courseType& right) const
{
	return (courseNo < right.courseNo);
}

bool courseType::operator>=(const courseType& right) const
{
	return (courseNo >= right.courseNo);
}

bool courseType::operator>(const courseType& right) const
{
	return (courseNo > right.courseNo);
}

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
108
109
110
111
112
113
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <vector>
#include <iterator>

#include "studentType.h"

using namespace std;

void getStudentData(ifstream& infile,
					vector<studentType> &studentList);

void printGradeReports(ofstream& outfile,
					   vector<studentType> studentList,
					   double tuitionRate);

int main()
{
	vector<studentType> studentList;

	double tuitionRate;

	ifstream infile;
	ofstream outfile;

	infile.open("c:\\Martin\\MalikDownloads\\Chapter 4 Source Code\\GradeReport2018\\ch4_GradeData.txt");

	if(!infile)
	{
	   cerr<<"Input file does not exist. "
  		   <<"Program terminates."<<endl;
   	   return 1;
	}

	outfile.open("c:\\Martin\\MalikDownloads\\Chapter 4 Source Code\\GradeReport2018\\sDataOut.txt");

	infile>>tuitionRate;		//get the tuition rate

	getStudentData(infile, studentList);
	printGradeReports(outfile, studentList, tuitionRate);

	infile.close();
	outfile.close();

	return 0;
}

void getStudentData(ifstream& infile,
					vector<studentType> &studentList)
{
		//Local variable
	string fName;	    //variable to store the first name
	string lName;	    //variable to store the last name
	int ID;				//variable to store the student ID
	int noOfCourses;	//variable to store the number of courses
	char isPaid;	    //variable to store Y/N, that is,
						//is tuition paid
	bool isTuitionPaid;	//variable to store true/false

	string cName;	//variable to store the course name
	string cNo;		//variable to store the course number
	int credits;	//variable to store the course credit hours
	char grade;		//variable to store the course grade

	int i;		//loop control variable

	vector<courseType> courses;	  //vector of objects to store course
							      //information
	courseType cTemp;
	studentType sTemp;

	infile>>fName;										//Step 1

	while(infile)
	{
		infile>>lName>>ID>>isPaid;						//Step 1

		if(isPaid == 'Y')								//Step 2
			isTuitionPaid = true;
		else
			isTuitionPaid = false;

		infile>>noOfCourses;							//Step 3

		courses.clear();								//Step 4

		for(i = 0; i < noOfCourses; i++)				//Step 5
		{
			infile>>cName>>cNo>>credits>>grade;			//Step 5.a
			cTemp.setCourseInfo(cName, cNo,
							   grade, credits);			//Step 5.b
			courses.push_back(cTemp);					//Step 5.c
		}

		sTemp.setInfo(fName, lName, ID, isTuitionPaid,
		 	 		   courses);						//Step 6
		studentList.push_back(sTemp);					//Step 7

		infile>>fName;									//Step 1
   }//end while
}

void printGradeReports(ofstream& outfile,
					   vector<studentType> studentList,
					   double tuitionRate)
{
   unsigned int count;

    for(count = 0; count < studentList.size(); count++)
	    studentList[count].print(outfile, tuitionRate);
}

Here is the sample input data:

345
Lisa Miller 890238 Y A
Mathematics MTH345 4 A
Physics PHY357 3 B
ComputerSci CSC478 3 B
History HIS356 3 A

Bill Wilton 798324 N 5
English ENG378 3 B
Philosophy PHL534 3 A
Chemistry CHM256 4 C
Biology BIO234 4 A
Mathematics MTH346 3 C

Dandy Goat 746333 Y 6
History HIS101 3 A
English ENG328 3 B
Mathematics MTH137 3 A
Chemistry CHM348 4 B
ComputerSci CSC201 3 B
Business BUS128 3 C


99,99% that’s because there’s an error in your input data at the second line: the last ‘A’ should be (I think) a ‘4’.
To check data consistency in a text file, just keep columns aligned, like so:

ch4_GradeData.txt:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
345
Lisa Miller 890238 Y 4
Mathematics MTH345 4 A
Physics     PHY357 3 B
ComputerSci CSC478 3 B
History     HIS356 3 A

Bill Wilton 798324 N 5
English     ENG378 3 B
Philosophy  PHL534 3 A
Chemistry   CHM256 4 C
Biology     BIO234 4 A
Mathematics MTH346 3 C

Dandy Goat 746333 Y 6
History     HIS101 3 A
English     ENG328 3 B
Mathematics MTH137 3 A
Chemistry   CHM348 4 B
ComputerSci CSC201 3 B
Business    BUS128 3 C


Let us know if that solves your issue. There are other weird things in that code, but I can’t know if the author deliberately wrote them to illustrate better solutions in the following chapters.
Here is an extract from the text book about the data:

The data is stored in a file in the following form:

tuitionRate
StudentName studentID isTuitionPaid numberOfCourses
courseName courseNumber creditHours grade
courseName courseNumber creditHours grade
.
.
.
The first line indicates that the tuition rate is $345 per credit hour. Next the course data
for the student Lisa Miller is given: Lisa Miller's ID is 890238, she has paid the tuition,
and is taking 4 courses. The course number for the mathemetics class she is taking is
MTH345, the course has 4 credit hours, her midsemester grade is A, and so on.

The desired output for each student is of the following form:

Student Name: Lisa Miller
Student ID: 890238
Number of courses enrolled: 4

Course No Course Name Credits Grade
CSC478 ComputerSci 3 B
HIS356 History 3 A
MTH345 Mathematics 4 A
PHY357 Physics 3 B

Total number of credits: 13
Midsemester GPA: 3.54


So I think that the data is organised OKAY !!!
The problem is on the calculation of number of courses enrolled. We get a 0 instead of 4
" " " " " " " credits. We get a 0 instead of 13
" " " " " " " Midsemester GPA. We get a gabbage output there instead of 3.54

I am still going to check those calculations
345
Lisa Miller 890238 Y A


The data is stored in a file in the following form:
tuitionRate
StudentName studentID isTuitionPaid numberOfCourses

So, at the end of the second row, there should be a number, not a character.
Arguably, that line should be:
Lisa Miller 890238 Y 4

Have you tried to change it that way and check the output?
ENOIZAT!!!! you are a star!!!!

I changed it as you said and it worked fine!!!

Thank you! Thank you!! Thank you!!!
Hi Bopaki,

You can use a debugger to fix this kind of problem yourself, even if you didn't spot the problem with the input file.

You first had a problem with this:

Number of courses enrolled: 0

With a debugger one can see where that value is set, then deduce why.

Using a debugger will save you days of looking at code, and you can fix all kinds of problems, as long as you can get the code to compile.

Good Luck !!
Last edited on
You very welcome, Bopaki. Happy coding.
Topic archived. No new replies allowed.