Can't figure out what's wrong, help! :^(

My program is running, but it's giving me results and information that's not what I wanted.

I can't seem to get this program to calculate the correct GPA or Total Units, it gives me a loop of
"-858993118-107374176.00"

for each of my students. I seem to be able to correctly output the Students' Names and ID.

Also, after my output I get this random group of letters
538DED48
and I have no idea where it's coming from.

I thought about initializing the SPoints, SUnits in the structure to 0, but I'm not sure if I can or how to.
If you could help guide me in the right direction I would greatly appreciate it.

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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#define Size 20

using namespace std;

struct Stud{
	string Name;
	long ID;
	float GPA, SPoints;
	int SUnits;
	char Grade;
};

struct PStud{
	Stud list[Size];
};

void Process_Data(ifstream, Stud[], PStud &, int &);
float Assign_GPA(char);
void Process(int, float, float &, int &);
bool Verify(int);
bool Open_File(ifstream &);
bool Open_File2(ofstream &);

int main()
{
	Stud TEMP[Size];
	PStud Stud;
	string Name, FName;
	ifstream Input;
	ofstream Output;
	int Num = 0;
	int counter = 0, SUnits = 0, Tot_Units = 0;
	float SPoints = 0, Tot_Points = 0, Tot_GPA;

	if (Open_File(Input))
	{
		if (Open_File2(Output))
		{
			Output << setprecision(2) << fixed << showpoint;
			cout << left << setw(27) << cout << left<<setw(27)<<"\n\nStudent Name" << setw(10)<<"ID "<<setw(10)<<"Units" <<setw(8)<<"GPA" <<endl;
			cout <<"====================================================\n";
			Output << left<<setw(27)<<"\n\nStudent Name" << setw(10)<<"ID "<<setw(10)<<"Units" <<setw(8)<<"GPA" <<endl;
			Output <<"====================================================\n";
			getline(Input,TEMP[Num].Name);
			while(!Input.eof())
			{
				Input >> TEMP[Num].ID;
				Process_Data(Input, TEMP, Stud, Num);
				cout << setw(25) << Name << setw(10) << TEMP[Num].ID << setw(10) << TEMP[Num].SUnits << setw(8) << TEMP[Num].GPA <<endl;
				Output << setprecision(2)<<fixed;
				Output << setw(25) << Name << setw(10) << TEMP[Num].ID << setw(10) << TEMP[Num].SUnits << setw(8) << TEMP[Num].GPA <<endl;
				counter++ ;
				Input.ignore(10,'\n');
				getline(Input,Name);
			}
			Tot_GPA = Tot_Points / Tot_Units;
			cout <<"==================================================\n";
			cout <<"Group of "<<counter<<setw(25)<<" Students Totals" <<setw(10)<< Tot_Units << setw(10)<< Tot_GPA  << endl << endl ; 
			Output <<"==================================================\n\n";
			Output <<"Group of "<<counter<<setw(25)<<" Students Totals" <<setw(10)<< Tot_GPA << setw(10)<< Tot_GPA  << endl << endl ; 
			cout << endl << endl ;
		}
	}
	return 0;
}

Functions:
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
void Process_Data(ifstream &Input, Stud temp[], PStud &list, int &Num, int &TotU, float &TotP){
	float Points, SPoints = 0, Tot_Points = 0;
	int Units, SUnits = 0, Tot_Units = 0;
	char Grade;
	Input >> Grade;
	while(Grade != '*')
	{
		Input >> Units;
		Points = Assign_GPA(Grade);
		Process(Units, Points, SPoints, SUnits);
		temp[Num].SUnits += SUnits;
		temp[Num].SPoints += SPoints;
		Input >> Grade;
	}
	Tot_Points += temp[Num].SPoints;
	Tot_Units += temp[Num].SUnits;
	if (temp[Num].SUnits > 0)
	{
		temp[Num].GPA = temp[Num].SPoints/temp[Num].SUnits;
		Num++;
	}
}

float Assign_GPA(char Grade)
{
	float score;
	if (Grade == 'A' || Grade == 'a')
		score = 4.0;
	else if(Grade == 'B' || Grade == 'b')
		score = 3.0;
	else if(Grade == 'C' || Grade == 'c')
		score = 2.0;
	else if(Grade == 'D' || Grade == 'd')
		score = 1.0;
	else if(Grade == 'F' || Grade == 'f')
		score = 0.0;
	else
		score = -1;
	return score;
}

void Process(int Units, float Points, float &SPoints, int &SUnits)
{
	if (Verify(Units) && Points >= 0)
	{
		SPoints = (SPoints + Points*Units);
		SUnits += Units;
	}

}

bool Verify(int Units)
{
	if (Units > 0 && Units <=5)
		return true;
	else
		return false;
}

bool Open_File(ifstream &Input)
{
	int counter = 0;
	string FName;
	do{
		counter++;
		Input.clear();
		cout << "Enter the input file name: ";
		getline(cin, FName);
		Input.open(FName.c_str());
	}while (Input.fail() && counter < 3);
	if (counter > 3)
	{
		cout <<"Bad file name.\nProgram Terminated.\n\n";
		return false;
	}
	return true;
}

bool Open_File2 (ofstream &Output)
{
	int counter = 0;
	string FName;
	do{
		counter++;
		Output.clear();
		cout << "Enter the output file name: ";
		getline(cin, FName);
		Output.open (FName.c_str());
	}while (Output.fail() && counter < 3);
	if (counter > 3)
	{
		cout << "Bad file name.\nProgram Terminated.\n\n";
		return false;
	}
	return true;
}
Last edited on
In your function Process_Data() you don't use int &TotU, float &TotP
Topic archived. No new replies allowed.