The following code will not output correctly. Could you please review it? Here is what I've been asked to do.
This program will include error trapping with try and catch.
Put a throw in each function which gets user input and throw a string "Bad Major" if a Major of 0 is entered. The input functions are specified in 2, 4 and 7 below.
Create a global structure as follows:
struct Student
{
char Name[30];
float GPA;
int Major;
};
In main create 2 instances of that structure. Call them S1 and S2.
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.
In main print the data stored in the structures S1 and S2 using cout.
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…)
Back in main print the data stored in the structure S2 using cout.
Now create an array of 2 structures in main. Call the array Students.
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.
Call the function GetStudents from main.
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.
Here is my code
#include<iostream>
#include<iomanip>
using namespace std;
//Global variable to determine size of name
const int Name_Size = 30;
//Structure
struct Student
{
char Name[30];
float GPA;
int Major;
};
//Function Prototypes
Student* studentData(Student &S1);
Student* changeData(Student &S2);
Student getStudent(Student array[], int size);
Student printStudents(Student array[], int size);
int main()
{
Student S1; // structure instance called S1.
Student S2; // structure instance called S2.
Student student[2]; //array of 2 structures.
S2 = *studentData(S1); //Calls the studentData function.
changeData(S2); //Calls the changeData function.
getStudent(&S1, 2); //Calls the getStudent function.
printStudents(&S1, 2); //Calls the printStudents function.
system("pause");
return 0;
}
//Function Definitions
//studentData function
Student* studentData(Student &S1)
{
cout << "Student 1" << endl;
cout << "_________" << endl;
cout << "Name: " << S1.Name << endl;
cout << "GPA: " << S1.GPA << endl;
cout << "Major: " << S1.Major << endl;
cout << endl;
if (S1.Major == 0)
{
throw "Bad Major!\n";
}
else
return &S1;
}
Student* changeData(Student &S2) // Changes the data.
{
cout << "Name: " << S2.Name << endl;
cout << "GPA: " << S2.GPA << endl;
cout << "Major: " << S2.Major << endl;
if (S2.Major == 0)
{
throw "Bad Major!\n";
}
else
return &S2;
}
Student getStudent(Student array[], int size) //Function and loop to receive the array and an int representing the number of elements(2).
{
for (int index = 0; index < size; index++)
{
cout << "Please enter students full name: ";
cin.getline(array[index].Name, 30);
cout << "Please enter students GPA: ";
cin >> array[index].GPA;
cout << "Please enter students major: ";
cin >> array[index].Major;
cin.ignore(1);
}
if (array[1].Major == 0 || array[2].Major == 0)
{
throw "Bad Major!\n";
}
else
return array[size];
}
Student printStudents(Student array[], int size)
{
for (int index = 0; index < size; index++)
{
cout << array[index].Name << " " << array[index].GPA << " " << array[index].Major << endl;
return array[size];
}
}