passing structures to functions

Ok i fixed most of all of the warnings...now i'm getting this one small warning where line 61 is not being initialized..I can't seem to find the problem of initializing those..Maybe if this is fixed..the output would come out correct...guidance please?

here's the warning: c:\users\tommeh\desktop\program 5\program 5\main.cpp(61): warning C4700: uninitialized local variable 'stud' used

I tried initializing stud.ID and stud.gpa like this
stud.ID=0;
stud.gpa=0;
i initialized those two in while loop along with the other variables that are initialized, but in the output i got all 0's in the ID and -1.$ in the gpa.

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 <iomanip>
#include <fstream>
#include <cmath>
#include <stdlib.h>
using namespace std;


struct Student
{
int ID;
double points;
double hours;
double gpa;
};
//function prototypes go here//

void printfile(ofstream &OutputFile);
double findPoints(int grade, double ch);
//double findHours(double ch);
double findGPA(Student);
int main()
{
	Student stud;
	//double gpa, points, hours;
	//int ID;
	int grade;
	double ch;
	
	ofstream OutputFile;
	OutputFile.open("Output.dat");

	ifstream infile;
	infile.open("Input.dat");

	printfile(OutputFile);

OutputFile<<fixed<<showpoint<<setprecision(1)<<endl;

infile>>stud.ID;    //Reads in ID by itself
while (!infile.eof())
{
	//initialize variables
	Student stud;
	stud.points=0;
	stud.hours=0;
	ch=0;
	grade=0;
infile>>grade>>ch;  //Reads in grade and credit hours
while(ch >=0)  //When flags or sentinel values are read, the loop terminates
{
	stud.points = stud.points + findPoints(grade, ch);
	stud.hours = stud.hours + ch;
	stud.gpa = findGPA(stud);
	infile>>grade>>ch;  //reads the next grade and credit hours
}
OutputFile<<setw(15)<<stud.ID<<setw(18)<<stud.gpa<<endl;

infile>>stud.ID;  //reads the next ID
}
	return 0; 

	OutputFile.close();
	infile.close();
}
//Function Definitions Go Here
void printfile(ofstream &OutputFile)
{
	OutputFile<<setw(30)<<"Thomas Powe"<<endl;
	OutputFile<<setw(29)<<"03-27-12"<<endl<<endl;
	OutputFile<<setw(15)<<"ID"<<setw(18)<<"GPA"<<endl<<endl;
return;
}

double findPoints(int grade, double ch)
{
	Student stud;
	stud.points=0;
	stud.points = stud.points + grade * ch;
	return stud.points;
}

//double findHours(double ch)
//{
	//Student stud;
	//Student hours;
	//stud.hours=0;
	//stud.hours = ch +ch;
	//return stud.hours;
//}
double findGPA(Student)
{
	Student stud;
	stud.points=0;
	stud.hours=0;
	stud.gpa= stud.points/stud.hours;
	return stud.gpa;
}
If you want it to be initialised, you have to define a constructor for your struct in which you initialise the variables:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
struct Student
{
   int ID;
   double points;
   double hours;
   double gpa;
   Student()
   {
      //this function is called each time the a Student object is created
      points = 0;
      hours = 0;
      gpa = 0;
      ID = 0;
   }
};


Cheers.
Last edited on
That findGPA function will return 0.0 every time.
BTW, use classes rather than structures. struct is the obsolete form for class in C. You're programming C++, so you better go with class. The only difference is that the access type for struct is public, while it's private in classes. So at the beginning of your class add

public:

and you should have exactly the same functionality, but with up to date style.
BTW, use classes rather than structures. struct is the obsolete form for class in C. You're programming C++, so you better go with class. The only difference is that the access type for struct is public, while it's private in classes. So at the beginning of your class add

public:

and you should have exactly the same functionality, but with up to date style.


Or throw some nice OOP principles (namely encapsulation) in there. Private member variables, public setter/getter methods.
Last edited on
Yep ;-)
can't use classes, our professor only want us to use what we learned already and that just data structures....
Ok the variables are initialized, but now, when i run the program, its only reading 1 ID number, and the GPA is still 1.$
As mentioned above already, in C++, a struct and a class are identical but for default privacy level. You ARE using classes already.
in findGPA(), you have to define the other values correctly. You're performing a 0/0 operation.

Please check your formulas and avoid assigning stuff directly like that in unctions. It doesn't make sense.
Last edited on
The code looks much better, and the output file is coming out almost right, but i'm still having a problem with the gpa's output: its all still coming out like -1.$.

does it have something to do with the variables, double grade, ch;??
because they are in the function prototype findPoints(double grade, double ch)
and stud.points is in the function definition, which the variable "points" is in the data structure

My professor ONLY wanted the variables gpa, points, hours, and ID to be in the structure, leaving the other variables "grade" and "ch" out.

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 <iomanip>
#include <fstream>
#include <cmath>
#include <stdlib.h>
using namespace std;


struct Student
{
int ID;
double points;
double hours;
double gpa;
	Student ()
	{
	ID=0;
	points=0;
	hours=0;
	gpa=0;
	}
};
//function prototypes go here//

void printfile(ofstream &OutputFile);
double findPoints(int grade, double ch);
//double findHours(double ch);
double findGPA(Student);
int main()
{
	Student stud;
	//double gpa, points, hours;
	//int ID;
	int grade;
	double ch;
	
	ofstream OutputFile;
	OutputFile.open("Output.dat");

	ifstream infile;
	infile.open("Input.dat");

	printfile(OutputFile);

OutputFile<<fixed<<showpoint<<setprecision(1)<<endl;

infile>>stud.ID;    //Reads in ID by itself
while (!infile.eof())
{
	//initialize variables
	ch=0;
	grade=0;
	infile>>grade>>ch;  //Reads in grade and credit hours
	while(ch >=0)  //When flags or sentinel values are read, the loop terminates
	{
	stud.points = stud.points + findPoints(grade, ch);
	stud.hours = stud.hours + ch;
	stud.gpa = findGPA(stud);
	infile>>grade>>ch;  //reads the next grade and credit hours
	}
	OutputFile<<setw(15)<<stud.ID<<setw(18)<<stud.gpa<<endl;
	infile>>stud.ID; //reads the next ID

}
	OutputFile.close();
	infile.close();
	return 0; 
}
//Function Definitions Go Here
void printfile(ofstream &OutputFile)
{	
	OutputFile<<setw(30)<<"Thomas Powe"<<endl;
	OutputFile<<setw(29)<<"03-27-12"<<endl<<endl;
	OutputFile<<setw(15)<<"ID"<<setw(18)<<"GPA"<<endl<<endl;
	return;
}

double findPoints(int grade, double ch)
{
	Student stud;
	stud.points = stud.points + grade * ch;
	return stud.points;
}

//double findHours(double ch)
//{
	//Student stud;
	//Student hours;
	//stud.hours=0;
	//stud.hours = ch +ch;
	//return stud.hours;
//}
double findGPA(Student)
{
	Student stud;
	stud.gpa= stud.points/stud.hours;
	return stud.gpa;
}
Last edited on
Try this for findGPA:
1
2
3
4
double findGPA(Student a)
{
   return a.points/a.hours;
}

Ok so i fixed the code and the output came out correct, but now I wanna take this
1
2
3
4
5
6
double findPoints(int grade, double ch)
{
	Student stud;
	stud.points = stud.points + grade * ch;
	return stud.points;
}


I want to turn it into a pass by reference, How will i write that??





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
#include <iomanip>
#include <fstream>
#include <cmath>
#include <stdlib.h>
using namespace std;


struct Student
{
int ID;
double points;
double hours;
double gpa;
	Student ()
	{
	ID=0;
	points=0;
	hours=0;
	//gpa=0;
	}
};
//function prototypes go here//

void printfile(ofstream &OutputFile);
double findPoints(int grade, double ch);
//double findHours(double ch);
double findGPA(Student stud);
int main()
{
	Student stud;
	//double gpa, points, hours;
	//int ID;
	int grade;
	double ch;
	
	ofstream OutputFile;
	OutputFile.open("Output.dat");

	ifstream infile;
	infile.open("Input.dat");

	printfile(OutputFile);

OutputFile<<fixed<<showpoint<<setprecision(1)<<endl;

infile>>stud.ID;    //Reads in ID by itself
while (!infile.eof())
{
	//initialize variables
	stud.points=0;
	stud.hours=0;
	ch=0;
	grade=0;
	infile>>grade>>ch;  //Reads in grade and credit hours
	while(ch >=0)  //When flags or sentinel values are read, the loop terminates
	{
	stud.points = stud.points + findPoints(grade, ch);
	stud.hours = stud.hours + ch;
	stud.gpa = findGPA(stud);
	infile>>grade>>ch; 
	//reads the next grade and credit hours
	}
	OutputFile<<setw(15)<<stud.ID<<setw(18)<<stud.gpa<<endl;
	infile>>stud.ID; //reads the next ID

}
	OutputFile.close();
	infile.close();
	return 0; 
}
//Function Definitions Go Here
void printfile(ofstream &OutputFile)
{	
	OutputFile<<setw(30)<<"Thomas Powe"<<endl;
	OutputFile<<setw(29)<<"03-27-12"<<endl<<endl;
	OutputFile<<setw(15)<<"ID"<<setw(18)<<"GPA"<<endl<<endl;
	return;
}

double findPoints(int grade, double ch)
{
	Student stud;
	stud.points = stud.points + grade * ch;
	return stud.points;
}

//double findHours(double ch)
//{
	//Student stud;
	//Student hours;
	//stud.hours=0;
	//stud.hours = ch +ch;
	//return stud.hours;
//}
double findGPA(Student stud)
{
	//Student stud;
	stud.gpa= stud.points/stud.hours;
	return stud.gpa;
}
Last edited on
Am I missing something or does this function make no sense??
1
2
3
4
5
6
double findPoints(int grade, double ch)
{
	Student stud;
	stud.points = stud.points + grade * ch;
	return stud.points;
}


because if a student object is created with 0 points - then
stud.points = stud.points + grade * ch; is just 0+ grade*ch.

So return stud.points; is just returning grade*ch.

So basically what you have doe is created a studed object
for no real reason, just to multiply the two function parameters together.

1
2
3
4
double findPoints(int grade, double ch)
{
	return grade*ch;
}


Last edited on
yes thats what i'm trying to fix...but i'm confused right now
Why do you want to pass by reference?

Guestgulkan's method would seem the most reasonable.
I'm not exactlu sure what sort of function the OP is truing to make - and I don't want towaste time guessing - so maybe if he give us a better explanation
of what he is trying to do then we will be able to help.
confirmed what guestgulkan's post said with my professor and she said the exact same thing..now the code is better and finished..Thanks!
Topic archived. No new replies allowed.