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:
Code:
#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 ChangeData 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;
}
}