Using TryCatch in functions

I am learning to use the Try Catch Throw but I am having a hard time getting all of the throws to work. I have to throw each of my functions with a string of Bad Major if a Major is 0. I can get only one of my functions to work. If anyone has a suggestions I would appreciate it.

Thanks,
R.

Here is my code:

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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include <iostream>
#include<string>
#include<iomanip>

using namespace std;


struct Student
{
	char Name[30];
	float GPA;
	int Major;
};


Student StudentData(Student &s);
void ChangeData(Student *s);
void GetStudents(Student st[], short size);
void PrintStudents(Student st[], short size);


int main()
{
	//#1 create 2 instances of that structure. Call them S1 and S2. 
	Student S1;
	Student S2;

	try
	{
	// S2 = StudentData( S1 ); //this is the call to the function
	S2 = StudentData(S1);


	//#3. print the data stored in the structures S1 and S2 using cout.
	cout << endl;
	cout << "Structor Student S2 info " << endl;
	cout << "Student Name:" << S2.Name << endl;
	cout << "Student GPA: " << S2.GPA << endl;
	cout << "Student Major: " << S2.Major << endl;

	cout << endl;

	cout << "Structor Student S1 info " << endl;
	cout << "Student Name: " << S1.Name << endl;
	cout << "Student GPA: " << S1.GPA << endl;
	cout << "Student Major: " << S1.Major << endl;


	cout << endl;

	
	//#4. Call a function named ChangeData with a pointer to S2 as the argument:
    //        ChangeData( &S2 ); //this is the call to the function

	ChangeData(&S2);
	
	
	//#5. Back in main print the data stored in the structure S2 using cout.
	cout <<"The student's name from ChangeData is: " << S2.Name << endl;
	cout <<"The student's GPA from ChangeData is: " << S2.GPA << endl;
	cout <<"The student's Major from ChangeData is: " << S2.Major << endl;


	cout << endl;

	//#6. Create an array of 2 structures in main. Call the array Students.
	Student Students[2];

	//#8. Call the function GetStudents from main.
	GetStudents(Students, 2);


	cout << endl;

	cout << "Here are the students from the GetStudents Function " << endl;
	cout << "====================================================" << endl;
	cout << "Number" << setw(20) << "Student Name" << setw(11) << "GPA" << setw(9) << "Major" << endl;

	//#9. Call the function PrintStudents
	PrintStudents(Students, 2);
	}
	catch(char *e)
	{
		cout << e << endl;
	}

	cout << endl;
	

	system("pause");
	return 0;
}



//#2. Create and call a function named StudentData:
//   S2 = StudentData( S1 ); //this is the call to the function
//The function receives as a parameter a reference to the structure 
//(prototyping will handle this) and will return a reference to the structure. 
//Use couts and cins for getting data from the user. For testing purposes, 
//change the data in S1 so that the GPA is 3.5 and the Major is 2. 
//Since you are to use cins for getting data from the user, you are the user 
//and just enter these values. After the call to the function both S1 and S2 
//will contain the same data.

Student StudentData(Student &s)
{

	if(s.Major == 0)
	{
		throw "Bad Major from StudentData function";
	}
	else
	{
	
	//Get the Student Name
	cout << "Enter the student name: ";
	cin.getline(s.Name, 31);

	//Get the Student GPA
	cout << "Enter the student GPA: ";
	cin >> s.GPA;

	//Get the Student Major
	cout << "Enter the student Major: "; 
	cin >> s.Major;

	cin.ignore();
	}
	
	return s;
	
}

//#4 . Call a function named ChangeData with a pointer to S2 as the argument:
//     ChangeData( &S2 ); //this is the call to the function
//Change the data in S2 so that the GPA is 3.0 and the Major is 1. 
//(Using these values for testing…)
void ChangeData(Student *s)
{

	if(s->Major == 0)
	{
		throw "Bad Major from ChangeData function";
	}
	else
	{
	s->GPA = 3.0;
	s->Major = 1;
	}

}

//#7. Create a function, GetStudents, which will receive the array and an int 
//representing the number of elements(2). In the function, loop through the data 
//and get all three fields from the user using cin, cin.getline and cout statements. 
//Organize like this: 
//                  for (...........) 
//                  { 
//                        cout prompt to user 
//                         cin.getline for name 
//                         cout prompt to user 
//                         cin for GPA
//                         cout promp to user
//                         cin for Major 
//                          cin.ignore(1); 
//                      }
//The problem is that a cin for a numeric value will leave the ENTER key in the 
//keyboard buffer and that is OK with cin and other numbers but not with strings, 
//thus we must remove it on our own. cin.ignore should handle this for us.
void GetStudents(Student st[], short size)
{

	for(int cnt = 0; cnt < size; cnt++)
	{
		
		if(st[cnt].Major == 0)
		{
		throw "Bad Major from GetStudents function";
		}
		else
		{
		
		cout << "Enter the student's name " << endl;
		cin.getline(st[cnt].Name, 30);

		cout << "Enter the student's GPA " << endl;
		cin >> st[cnt].GPA;

		cout << "Enter the student's Major " << endl;
		cin >> st[cnt].Major;
		}

		cin.ignore();

	}

}


//9. Create a function, PrintStudents, which will receive the same arguments as GetStudents. 
//It will print out the array of students on 2 lines, 1 line per student.
void PrintStudents(Student st[], short size)
{
	for(int cnt = 0; cnt < size; cnt++)
	{
		cout <<"#" << (cnt + 1) << setw(25) << st[cnt].Name << setw(10) << st[cnt].GPA << setw(7) << st[cnt].Major << endl;
	}
}
Last edited on
Topic archived. No new replies allowed.